From: Kimonas Fountoulakis on
Hi

I have a mex file and I am trying to use some Matlab function in it by using the mxCallMATLAB function.

A part of my program is:


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "../../interface/Lp.h"
#include "../../interface/mps.h"
#include "myallocator.h"
#include "mex.h"


/*
A complete example of how to solve an LP problem with HOPDM.
The LP problem is loaded directly into HOPDM data structures.

The problem
min -3 * x1 + 5 * x2
s. to x1 + 6 * x2 <= 6
-10 <= x1 <= 20
0 <= x2 <= 30
has unique solution
x1 = 6, x2 = 0, f(x_opt) = -18.0
*/



/*-------------------- solve simple LP problem --------------------*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{

long max_rows = mxGetScalar(prhs[0]); /* 10 */
long max_cols = mxGetScalar(prhs[1]); /* 20 */
long max_nzA = mxGetScalar(prhs[2]); /* 50 */
long max_nzQ = mxGetScalar(prhs[3]); /* 50 */

long presolve = mxGetScalar(prhs[4]); /* 0 */
long irobj = mxGetScalar(prhs[5]); /* -1 */
long iolog = mxGetScalar(prhs[6]); /* 77 */
long restart = mxGetScalar(prhs[7]); /* 0 */
long iexit;
long iters;
double opt_tol;
long errorb;
long erroru;
long errorc;
long asmall;
long alarge;
double mult = mxGetScalar(prhs[16]); /* 1. */
long big = mxGetScalar(prhs[17]); /* 1.e30 */
long z_opt;
int i;
int code;

MPS *P;
Lp *my_Pb;
Lp_save *L_save;
L_save = VECT (Lp_save, 1);


mxArray *lhs[5];
mxArray *rhs;
rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
double *x = mxGetPr(rhs);

x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0; x[6] = 0;

mexCallMATLAB(5, &lhs, 6, &rhs, "getData"); /* HERE IS THE WARNING */

/*
mexCallMATLAB(0, NULL, 1, &rhs, "disp");
*/

the function that I am trying to use is:
[A,b,xs,xsn,picks] = getData(m,n,k,Ameth,xmeth,varargin)

and the warning is:

MexSmallLP1.c:82: warning: passing argument 2 of &#8216;mexCallMATLAB&#8217; from incompatible pointer type
/home/kfoynt/Desktop/matlab/extern/include/mex.h:252: note: expected &#8216;struct mxArray **&#8217; but argument is of type &#8216;struct mxArray * (*)[5]&#8217;

After I am trying to use my MEX file, the matlab crashes and forces me to restart it. If I remove the line 82 everything works fine!
From: James Tursa on
"Kimonas Fountoulakis" <kfoynt(a)hotmail.com> wrote in message <hujifh$7t9$1(a)fred.mathworks.com>...
>
> mxArray *lhs[5];
> mxArray *rhs;
> rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
> double *x = mxGetPr(rhs);
:
> mexCallMATLAB(5, &lhs, 6, &rhs, "getData"); /* HERE IS THE WARNING */
:
> mexCallMATLAB(0, NULL, 1, &rhs, "disp");
>
> the function that I am trying to use is:
> [A,b,xs,xsn,picks] = getData(m,n,k,Ameth,xmeth,varargin)

To make the above call correctly, set things up as follows:

int i;
mxArray *lhs[5];
mxArray *rhs[6];
rhs[0] = whatever;
:
rhs[5] = whatever;
:
mexCallMATLAB(5, lhs, 6, rhs, "getData");
:
mexCallMATLAB(0, NULL, 1, lhs, "disp"); // display 1st output item
:
for( i=0; i<5; i++ ) {
mxDestroyArray(lhs[i]);
}
for( i=0; i<6; i++ ) {
mxDestroyArray(rhs[i]);
}


James Tursa
From: James Tursa on
"Kimonas Fountoulakis" <kfoynt(a)hotmail.com> wrote in message <hujifh$7t9$1(a)fred.mathworks.com>...
>
> mxArray *lhs[5];
> mxArray *rhs;
> rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
> double *x = mxGetPr(rhs);
>
> x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0; x[6] = 0;
>
> mexCallMATLAB(5, &lhs, 6, &rhs, "getData"); /* HERE IS THE WARNING */

I just noticed that it appears you are trying to use the pr data area of rhs[0] as the 6 inputs (although you tried to fill 7 of them) to your getData function. You can't do it this way. You have to create them individually. e.g.,

mxArray *lhs[5];
mxArray *rhs[6];
:
rhs[0] = mxCreateDoubleScalar(2);
rhs[1] = mxCreateDoubleScalar(2);
rhs[2] = mxCreateDoubleScalar(2);
rhs[3] = mxCreateDoubleScalar(0);
rhs[4] = mxCreateDoubleScalar(0);
rhs[5] = mxCreateDoubleScalar(0);
:
mexCallMATLAB(5, lhs, 6, rhs, "getData");

James Tursa
From: Kimonas Fountoulakis on
Thank you my friend.. but... it didnt work. I am giving you again my code:

mxArray *lhs[5];
mxArray *rhs;

rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
double *x = mxGetPr(rhs);


x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0;

mexCallMATLAB(5, lhs, 6, rhs, "getData");

/*
mexCallMATLAB(0, NULL, 1, &rhs, "disp");
*/

It gives me the warning for the second argument of the first mexCallMATLAB call. It compiles normally but if I try to run my program the matlab crashes. I tested the rhs variable by printing it (see /* comments */ ) and it works fine, the problem for the lhs variable.


"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hujmug$bpv$1(a)fred.mathworks.com>...
> "Kimonas Fountoulakis" <kfoynt(a)hotmail.com> wrote in message <hujifh$7t9$1(a)fred.mathworks.com>...
> >
> > mxArray *lhs[5];
> > mxArray *rhs;
> > rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
> > double *x = mxGetPr(rhs);
> :
> > mexCallMATLAB(5, &lhs, 6, &rhs, "getData"); /* HERE IS THE WARNING */
> :
> > mexCallMATLAB(0, NULL, 1, &rhs, "disp");
> >
> > the function that I am trying to use is:
> > [A,b,xs,xsn,picks] = getData(m,n,k,Ameth,xmeth,varargin)
>
> To make the above call correctly, set things up as follows:
>
> int i;
> mxArray *lhs[5];
> mxArray *rhs[6];
> rhs[0] = whatever;
> :
> rhs[5] = whatever;
> :
> mexCallMATLAB(5, lhs, 6, rhs, "getData");
> :
> mexCallMATLAB(0, NULL, 1, lhs, "disp"); // display 1st output item
> :
> for( i=0; i<5; i++ ) {
> mxDestroyArray(lhs[i]);
> }
> for( i=0; i<6; i++ ) {
> mxDestroyArray(rhs[i]);
> }
>
>
> James Tursa
From: Kimonas Fountoulakis on
It worked!!!!!!! THANKS!!

pls, coud you explain me why this doesn't work??

mxArray *lhs[5];
mxArray *rhs;

rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
double *x = mxGetPr(rhs);


x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0;


mexCallMATLAB(5, lhs, 6, rhs, "getData");

Is there a problem if I declare rhs as a matrix pass it into the x pointer, and pass the values into the memory through x??? and if yes, why??


Thanks again!!


"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hujnes$797$1(a)fred.mathworks.com>...
> "Kimonas Fountoulakis" <kfoynt(a)hotmail.com> wrote in message <hujifh$7t9$1(a)fred.mathworks.com>...
> >
> > mxArray *lhs[5];
> > mxArray *rhs;
> > rhs = mxCreateDoubleMatrix(6, 1, mxREAL);
> > double *x = mxGetPr(rhs);
> >
> > x[0] = 2; x[1] = 2; x[2] = 2; x[3] = 0; x[4] = 0; x[5] = 0; x[6] = 0;
> >
> > mexCallMATLAB(5, &lhs, 6, &rhs, "getData"); /* HERE IS THE WARNING */
>
> I just noticed that it appears you are trying to use the pr data area of rhs[0] as the 6 inputs (although you tried to fill 7 of them) to your getData function. You can't do it this way. You have to create them individually. e.g.,
>
> mxArray *lhs[5];
> mxArray *rhs[6];
> :
> rhs[0] = mxCreateDoubleScalar(2);
> rhs[1] = mxCreateDoubleScalar(2);
> rhs[2] = mxCreateDoubleScalar(2);
> rhs[3] = mxCreateDoubleScalar(0);
> rhs[4] = mxCreateDoubleScalar(0);
> rhs[5] = mxCreateDoubleScalar(0);
> :
> mexCallMATLAB(5, lhs, 6, rhs, "getData");
>
> James Tursa
 |  Next  |  Last
Pages: 1 2
Prev: randomly moving spheres
Next: hmm, initial matrix