From: Paolo on
Hello,

I have created a mex file which has embedded a c++ function which needs as input a matrix (nxm) with both n and m >1.
With mxArray I can get the matrix I pass to mex file and then I pass the mxArray (let's say x) to the c++ function by using a double*.

let's say that we have this c++ function
void fun(double *x, double *y)
x is the mxArray I pass to it but my problem is:
how can I go throught each element of x if I have x defined as double*?

can I define mxArray **x? or do I need to define it only as *x?

I hope you can undersatand my problem.
Thanks
P
From: James Tursa on
"Paolo " <tarpanelli(a)libero.it> wrote in message <i29vv9$mso$1(a)fred.mathworks.com>...
>
> I have created a mex file which has embedded a c++ function which needs as input a matrix (nxm) with both n and m >1.
> With mxArray I can get the matrix I pass to mex file and then I pass the mxArray (let's say x) to the c++ function by using a double*.
>
> let's say that we have this c++ function
> void fun(double *x, double *y)
> x is the mxArray I pass to it but my problem is:
> how can I go throught each element of x if I have x defined as double*?
>
> can I define mxArray **x? or do I need to define it only as *x?
>
> I hope you can undersatand my problem.

No, we don't. Most of what you have written indicates that you likely have several fundamental problems with your mex routine. Rather than speculate on all of that and write tutorials for you, I would request that you please post your code and tell us what, specifically, you are trying to do (e.g., "I want to call the function like this at the MATLAB command line and internally in the mex routine I want to call this C++ function", etc.)

James Tursa
From: Paolo on
Sorry James,
I try to be clear: this is my mex file in c++

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
mxArray *inDates = prhs[0];
double nDates = mxGetScalar(prhs[1]);
mxArray *inInstruments = prhs[2];
double nInst = mxGetScalar(prhs[3]);
double dcf = mxGetScalar(prhs[4]);
double roll = mxGetScalar(prhs[5]);
double cal = mxGetScalar(prhs[6]);
double interp = mxGetScalar(prhs[7]);

plhs[0]=mxCreateDoubleMatrix(20,1,mxREAL);

myfun(mxGetPr(inDates), (int)nDates, mxGetPr(inInstruments) , (int)nInst, (int)dcf, (int)roll, (int)cal, (int) interp, mxGetPr(plhs[0]));

}

As input I have 2 matrix and 6 scalars. Then I want to pass these inputs to a c++ function (myfun) which is:

void myfun(double *refdate,
int nDates,
double **instruments,
int nInst,
int daycounter,
int rollconv,
int calnum,
int interp,
double *oDF)
{
bla bla bla......
oDF[0]=instruments[0][2] * .....+..... bla bla....
}

I am sure I missed something from the mex guide but my problem is how to pass, to myfun, the input instrument as a double pointer instead of a single pointer given that inside myfun I will go through each element of the row i and column j (e.g instruments[i][j]).

I tried to pass instruments as single array and defined instruments as single pointer and it works, but is not what I want! So, may be I am wrong, but I suppose the problem is how to declare the input instrument inside the mex file and how to pass it correctly to the c++ function,

Thank you in advance,

"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2a580$1v5$1(a)fred.mathworks.com>...
> "Paolo " <tarpanelli(a)libero.it> wrote in message <i29vv9$mso$1(a)fred.mathworks.com>...
> >
> > I have created a mex file which has embedded a c++ function which needs as input a matrix (nxm) with both n and m >1.
> > With mxArray I can get the matrix I pass to mex file and then I pass the mxArray (let's say x) to the c++ function by using a double*.
> >
> > let's say that we have this c++ function
> > void fun(double *x, double *y)
> > x is the mxArray I pass to it but my problem is:
> > how can I go throught each element of x if I have x defined as double*?
> >
> > can I define mxArray **x? or do I need to define it only as *x?
> >
> > I hope you can undersatand my problem.
>
> No, we don't. Most of what you have written indicates that you likely have several fundamental problems with your mex routine. Rather than speculate on all of that and write tutorials for you, I would request that you please post your code and tell us what, specifically, you are trying to do (e.g., "I want to call the function like this at the MATLAB command line and internally in the mex routine I want to call this C++ function", etc.)
>
> James Tursa
From: James Tursa on
"Paolo " <tarpanelli(a)libero.it> wrote in message <i2bbcm$4df$1(a)fred.mathworks.com>...
> Sorry James,
> I try to be clear: this is my mex file in c++
>
> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
> {
> mxArray *inDates = prhs[0];
> double nDates = mxGetScalar(prhs[1]);
> mxArray *inInstruments = prhs[2];
> double nInst = mxGetScalar(prhs[3]);
> double dcf = mxGetScalar(prhs[4]);
> double roll = mxGetScalar(prhs[5]);
> double cal = mxGetScalar(prhs[6]);
> double interp = mxGetScalar(prhs[7]);
>
> plhs[0]=mxCreateDoubleMatrix(20,1,mxREAL);
>
> myfun(mxGetPr(inDates), (int)nDates, mxGetPr(inInstruments) , (int)nInst, (int)dcf, (int)roll, (int)cal, (int) interp, mxGetPr(plhs[0]));
>
> }
>
> As input I have 2 matrix and 6 scalars. Then I want to pass these inputs to a c++ function (myfun) which is:
>
> void myfun(double *refdate,
> int nDates,
> double **instruments,
> int nInst,
> int daycounter,
> int rollconv,
> int calnum,
> int interp,
> double *oDF)
> {
> bla bla bla......
> oDF[0]=instruments[0][2] * .....+..... bla bla....
> }
>
> I am sure I missed something from the mex guide but my problem is how to pass, to myfun, the input instrument as a double pointer instead of a single pointer given that inside myfun I will go through each element of the row i and column j (e.g instruments[i][j]).
>
> I tried to pass instruments as single array and defined instruments as single pointer and it works, but is not what I want! So, may be I am wrong, but I suppose the problem is how to declare the input instrument inside the mex file and how to pass it correctly to the c++ function,

Thanks for posting your code. What you are attempting to do is to pass some type of argument to a C++ function and have that argument interpreted inside the function as a dynamically dimensioned 2D array. You can't do that directly. This language feature is known as conforming array sizes in Fortran (which has had this capability for decades) and variable length arrays (VLA) in C and is part of the C99 standard. Many popular C compilers presently support VLA but some do not. Unfortunately, the C++ standard does not support VLA at all. For C the basic argument syntax is like this:

void myfun(int m, int n, double x[m][n]);

Then inside myfun you can do the regular x[i][j] syntax to get at the values. Note that the third argument is not double **x like you are attempting. To get *that* syntax to work you would have to dynamically create an array of pointers that point to the rows and pass that to the function.

So I see your options as follows:

1) Use the double * syntax and treat the array linearly inside the function. You have to manually do the indexing arithmetic. A bit of a pain, but not that bad. You can define a macro to do it for you and get your code to *look* somewhat like 2D indexing inline. You will have to manually pass in the array sizes to your function. Sounds like you already know how to do this and have it working (except the macro part).

2) See if your compiler supports VLA and use that. May not be portable across some compilers though.

3) Create a dynamically allocated array of type double ** and fill that in run time with the appropriate addresses.

For all of these you need to realize that MATLAB stores 2D matrices in column order and C/C++ stores 2D matrices by row order, so it will look like the transpose of the matrix on the C/C++ side.

James Tursa
From: Paolo on
Thanks James for your help,
I got it :),

Kind Regards,
P

"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2be6l$q2k$1(a)fred.mathworks.com>...
> "Paolo " <tarpanelli(a)libero.it> wrote in message <i2bbcm$4df$1(a)fred.mathworks.com>...
> > Sorry James,
> > I try to be clear: this is my mex file in c++
> >
> > void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
> > {
> > mxArray *inDates = prhs[0];
> > double nDates = mxGetScalar(prhs[1]);
> > mxArray *inInstruments = prhs[2];
> > double nInst = mxGetScalar(prhs[3]);
> > double dcf = mxGetScalar(prhs[4]);
> > double roll = mxGetScalar(prhs[5]);
> > double cal = mxGetScalar(prhs[6]);
> > double interp = mxGetScalar(prhs[7]);
> >
> > plhs[0]=mxCreateDoubleMatrix(20,1,mxREAL);
> >
> > myfun(mxGetPr(inDates), (int)nDates, mxGetPr(inInstruments) , (int)nInst, (int)dcf, (int)roll, (int)cal, (int) interp, mxGetPr(plhs[0]));
> >
> > }
> >
> > As input I have 2 matrix and 6 scalars. Then I want to pass these inputs to a c++ function (myfun) which is:
> >
> > void myfun(double *refdate,
> > int nDates,
> > double **instruments,
> > int nInst,
> > int daycounter,
> > int rollconv,
> > int calnum,
> > int interp,
> > double *oDF)
> > {
> > bla bla bla......
> > oDF[0]=instruments[0][2] * .....+..... bla bla....
> > }
> >
> > I am sure I missed something from the mex guide but my problem is how to pass, to myfun, the input instrument as a double pointer instead of a single pointer given that inside myfun I will go through each element of the row i and column j (e.g instruments[i][j]).
> >
> > I tried to pass instruments as single array and defined instruments as single pointer and it works, but is not what I want! So, may be I am wrong, but I suppose the problem is how to declare the input instrument inside the mex file and how to pass it correctly to the c++ function,
>
> Thanks for posting your code. What you are attempting to do is to pass some type of argument to a C++ function and have that argument interpreted inside the function as a dynamically dimensioned 2D array. You can't do that directly. This language feature is known as conforming array sizes in Fortran (which has had this capability for decades) and variable length arrays (VLA) in C and is part of the C99 standard. Many popular C compilers presently support VLA but some do not. Unfortunately, the C++ standard does not support VLA at all. For C the basic argument syntax is like this:
>
> void myfun(int m, int n, double x[m][n]);
>
> Then inside myfun you can do the regular x[i][j] syntax to get at the values. Note that the third argument is not double **x like you are attempting. To get *that* syntax to work you would have to dynamically create an array of pointers that point to the rows and pass that to the function.
>
> So I see your options as follows:
>
> 1) Use the double * syntax and treat the array linearly inside the function. You have to manually do the indexing arithmetic. A bit of a pain, but not that bad. You can define a macro to do it for you and get your code to *look* somewhat like 2D indexing inline. You will have to manually pass in the array sizes to your function. Sounds like you already know how to do this and have it working (except the macro part).
>
> 2) See if your compiler supports VLA and use that. May not be portable across some compilers though.
>
> 3) Create a dynamically allocated array of type double ** and fill that in run time with the appropriate addresses.
>
> For all of these you need to realize that MATLAB stores 2D matrices in column order and C/C++ stores 2D matrices by row order, so it will look like the transpose of the matrix on the C/C++ side.
>
> James Tursa