Thread Subject:
mxcreatedoublematrix pointers help

Subject: mxcreatedoublematrix pointers help

From: Ryan

Date: 8 Aug, 2012 16:05:17

Message: 1 of 7

I want to have a 2 column matrix output from my MEX function, but I am not sure how to declare the pointer to the Output of the mxCreateDoubleMatrix call. See Below...


#define no_of_people_in prhs[0]
#define Calcs plhs[0]

mex_function_example(){

double **Calcs_ptr;
m = mxGetM(no_of_people_in);
n = mxGetN(no_of_people_in);

 /* Create a matrix for the return argument */
    Calcs = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);

/* Ouput*/
    Calcs_ptr = mxGetPr(Calcs);

I want to then pass Calcs_ptr to my main function like

main(...,....,Calcs_ptr,...)

So that it will return filled with the output data.

My question is, if I need to declare Calcs_ptr as

double **Calcs_ptr;
or
double *Calcs_ptr;

Since it is a two dimensional matrix.

Thanks!

Subject: mxcreatedoublematrix pointers help

From: Bruno Luong

Date: 8 Aug, 2012 17:53:13

Message: 2 of 7

"Ryan " <surfboard3r@aol.com> wrote in message <jvu2nt$c8g$1@newscl01ah.mathworks.com>...
>
> My question is, if I need to declare Calcs_ptr as
>
> double **Calcs_ptr;
> or
> double *Calcs_ptr;
>

double *Calcs_ptr;

Data are ranged in a contiguous memory, column-wise

Bruno

Subject: mxcreatedoublematrix pointers help

From: James Tursa

Date: 8 Aug, 2012 18:29:10

Message: 3 of 7

"Ryan " <surfboard3r@aol.com> wrote in message <jvu2nt$c8g$1@newscl01ah.mathworks.com>...
> I want to have a 2 column matrix output from my MEX function, but I am not sure how to declare the pointer to the Output of the mxCreateDoubleMatrix call. See Below...
>
>
> #define no_of_people_in prhs[0]
> #define Calcs plhs[0]
>
> mex_function_example(){
>
> double **Calcs_ptr;
> m = mxGetM(no_of_people_in);
> n = mxGetN(no_of_people_in);
>
> /* Create a matrix for the return argument */
> Calcs = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
>
> /* Ouput*/
> Calcs_ptr = mxGetPr(Calcs);
>
> I want to then pass Calcs_ptr to my main function like
>
> main(...,....,Calcs_ptr,...)
>
> So that it will return filled with the output data.
>
> My question is, if I need to declare Calcs_ptr as
>
> double **Calcs_ptr;
> or
> double *Calcs_ptr;
>
> Since it is a two dimensional matrix.
>
> Thanks!

Please show the complete signature (i.e., how the arguments are declared) for the main(...,....,Calcs_ptr,...) call. In general, if main is expecting a 2D array for this argument, then you will typically need to create a dynamically allocated array off to the side and copy the transposed data to this memory, etc. etc. It is not particularly pretty but it can be made to work. Alternatively it is often easier to rewrite the called routine (main in this case) so that it works with a 1D input instead of a 2D input and then do the 2D indexing manually yourself. Once you post the complete signature for main we can advise.

James Tursa

Subject: mxcreatedoublematrix pointers help

From: Ryan

Date: 8 Aug, 2012 19:01:17

Message: 4 of 7

"James Tursa" wrote in message <jvub5m$dq9$1@newscl01ah.mathworks.com>...
> "Ryan " <surfboard3r@aol.com> wrote in message <jvu2nt$c8g$1@newscl01ah.mathworks.com>...
> > I want to have a 2 column matrix output from my MEX function, but I am not sure how to declare the pointer to the Output of the mxCreateDoubleMatrix call. See Below...
> >
> >
> > #define no_of_people_in prhs[0]
> > #define Calcs plhs[0]
> >
> > mex_function_example(){
> >
> > double **Calcs_ptr;
> > m = mxGetM(no_of_people_in);
> > n = mxGetN(no_of_people_in);
> >
> > /* Create a matrix for the return argument */
> > Calcs = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
> >
> > /* Ouput*/
> > Calcs_ptr = mxGetPr(Calcs);
> >
> > I want to then pass Calcs_ptr to my main function like
> >
> > main(...,....,Calcs_ptr,...)
> >
> > So that it will return filled with the output data.
> >
> > My question is, if I need to declare Calcs_ptr as
> >
> > double **Calcs_ptr;
> > or
> > double *Calcs_ptr;
> >
> > Since it is a two dimensional matrix.
> >
> > Thanks!
>
> Please show the complete signature (i.e., how the arguments are declared) for the main(...,....,Calcs_ptr,...) call. In general, if main is expecting a 2D array for this argument, then you will typically need to create a dynamically allocated array off to the side and copy the transposed data to this memory, etc. etc. It is not particularly pretty but it can be made to work. Alternatively it is often easier to rewrite the called routine (main in this case) so that it works with a 1D input instead of a 2D input and then do the 2D indexing manually yourself. Once you post the complete signature for main we can advise.
>
> James Tursa




Well I have decided to just return two separate vectors instead of one. But, I am running into some problems with data types that Im just not sure about....

in my C++ code, I try
plhs[0] = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);

vector<double> *a_ptr = (vector<double> *)mxGetData(plhs[0]);

My question is, does it work to just export a type vector< double> to a MATLAB double matrix? How do I return a variable of type vector<double> to MATLAB?

Thanks

Subject: mxcreatedoublematrix pointers help

From: James Tursa

Date: 8 Aug, 2012 19:37:13

Message: 5 of 7

"Ryan " <surfboard3r@aol.com> wrote in message <jvud1t$klk$1@newscl01ah.mathworks.com>...
>
> Well I have decided to just return two separate vectors instead of one. But, I am running into some problems with data types that Im just not sure about....
>
> in my C++ code, I try
> plhs[0] = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
>
> vector<double> *a_ptr = (vector<double> *)mxGetData(plhs[0]);
>
> My question is, does it work to just export a type vector< double> to a MATLAB double matrix? How do I return a variable of type vector<double> to MATLAB?

Are you married to the concept of using vector<double> in your code for some reason? E.g., why not just work with this instead:

double *a_ptr = mxGetPr(plhs[0]);

P.S. You don't need to explicitly cast (mwSize)m and (mwSize)n in the mxCreateDoubleMatrix function call ... the prototype & compiler will take care of that automatically. You can use simply m and n.

James Tursa

Subject: mxcreatedoublematrix pointers help

From: Ryan

Date: 8 Aug, 2012 19:49:15

Message: 6 of 7

"James Tursa" wrote in message <jvuf59$rkb$1@newscl01ah.mathworks.com>...
> "Ryan " <surfboard3r@aol.com> wrote in message <jvud1t$klk$1@newscl01ah.mathworks.com>...
> >
> > Well I have decided to just return two separate vectors instead of one. But, I am running into some problems with data types that Im just not sure about....
> >
> > in my C++ code, I try
> > plhs[0] = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
> >
> > vector<double> *a_ptr = (vector<double> *)mxGetData(plhs[0]);
> >
> > My question is, does it work to just export a type vector< double> to a MATLAB double matrix? How do I return a variable of type vector<double> to MATLAB?
>
> Are you married to the concept of using vector<double> in your code for some reason? E.g., why not just work with this instead:
>
> double *a_ptr = mxGetPr(plhs[0]);
>
> P.S. You don't need to explicitly cast (mwSize)m and (mwSize)n in the mxCreateDoubleMatrix function call ... the prototype & compiler will take care of that automatically. You can use simply m and n.
>
> James Tursa





Well the reason i am using vector<double> instead of double is for the added abilities C++ allows.

for instance:
vector<double> a

then i can say

a.size()

whereas i couldnt for just type double.

Subject: mxcreatedoublematrix pointers help

From: James Tursa

Date: 8 Aug, 2012 22:22:12

Message: 7 of 7

"Ryan " <surfboard3r@aol.com> wrote in message <jvufrr$tc$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <jvuf59$rkb$1@newscl01ah.mathworks.com>...
> > "Ryan " <surfboard3r@aol.com> wrote in message <jvud1t$klk$1@newscl01ah.mathworks.com>...
> > >
> > > Well I have decided to just return two separate vectors instead of one. But, I am running into some problems with data types that Im just not sure about....
> > >
> > > in my C++ code, I try
> > > plhs[0] = mxCreateDoubleMatrix( (mwSize)m, (mwSize)n, mxREAL);
> > >
> > > vector<double> *a_ptr = (vector<double> *)mxGetData(plhs[0]);
> > >
> > > My question is, does it work to just export a type vector< double> to a MATLAB double matrix? How do I return a variable of type vector<double> to MATLAB?
> >
> > Are you married to the concept of using vector<double> in your code for some reason? E.g., why not just work with this instead:
> >
> > double *a_ptr = mxGetPr(plhs[0]);
> >
> > P.S. You don't need to explicitly cast (mwSize)m and (mwSize)n in the mxCreateDoubleMatrix function call ... the prototype & compiler will take care of that automatically. You can use simply m and n.
> >
> > James Tursa
>
>
>
>
>
> Well the reason i am using vector<double> instead of double is for the added abilities C++ allows.
>
> for instance:
> vector<double> a
>
> then i can say
>
> a.size()
>
> whereas i couldnt for just type double.

Then you can't do what you are trying to do ... i.e., just pass the address of the MATLAB memory into what gets created by the vector<double> template. By using this template you are letting the compiler do all the memory stuff behind the scenes for you as far as allocation, reallocation, etc. You can't just "point" the vector to the MATLAB memory as you seem to be trying. If you want to stick with the vector<double> stuff then you will have to copy the MATLAB data into and out of it.

James Tursa

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
mex Ryan 8 Aug, 2012 12:09:19
mxcreatedobulematr... Ryan 8 Aug, 2012 12:09:19
pointers Ryan 8 Aug, 2012 12:09:19
c Ryan 8 Aug, 2012 12:09:19
mex_function Ryan 8 Aug, 2012 12:09:19
rssFeed for this Thread

Contact us