Thread Subject: mexCallMATLAB populating inputs

Subject: mexCallMATLAB populating inputs

From: John Harrold

Date: 19 Aug, 2009 20:27:00

Message: 1 of 16

Howdy folks,

I had a class in C sometime in the previous century, but truth be told
I never really understood pointers. I've recently come to a point
where I need to call matlab m-files from some ode simulations I'm
running, and I've been trying to incorporate the mexCallMATLAB
function into these simulations. I've tried to create a simple example
to figure this out. As an example I'd like to call the following m-
file from within a mex function:

<mymfile.m>
function [output]=mymfile(input)

output = input(1)+2*input(2)-input(3)^3 + input(4);
</mymfile.m>

So if I call this from within matlab I get the following:

>> mymfile([1 2 3 4])

ans =

   -18

Now I've constructed the following mex file

<mexfile.c>
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[] )
{
  int nlhs1, nrhs1;
  mxArray *plhs1[1], *prhs1;
  prhs1 = mxCreateDoubleMatrix(4, 1, mxREAL);


  /* how do I populate prhs1? */

  nlhs1 = 1; nrhs1 = 1;
  mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"mymfile");
}
</mexfile.c>

As the comment above indicates. I want to know how to go about
populating the prhs1 with my input (in this case 1 2 3 4). I
apologize for the simple question here, I'm just at a loss with this.

Subject: mexCallMATLAB populating inputs

From: James Tursa

Date: 19 Aug, 2009 22:31:20

Message: 2 of 16

You are making this particular example too hard. No need to make a copy of the input, just pass it on to the mexCallMATLAB as is. And no need to make a separate output variable, just use plhs directly. Try this:

#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[] )
{
  mexCallMATLAB(1, plhs, 1, prhs, "mymfile");
}

>> a=[1 2 3 4]
a =
     1 2 3 4
>> mymfile(a)
ans =
   -18
>> mymfilemex(a)
ans =
   -18

James Tursa

Subject: mexCallMATLAB populating inputs

From: John Harrold

Date: 20 Aug, 2009 00:16:50

Message: 3 of 16

On Aug 19, 6:31 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> You are making this particular example too hard. No need to make a copy of the input, just pass it on to the mexCallMATLAB as is. And no need to make a separate output variable, just use plhs directly. Try this:

I'm making this example hard because it's what I'm actually going to
have to do in my code. So I tried to provide a minimal example that,
once my question is answered, will enable me to accomplish what I want
to do. So to be a little more explicit:

What syntax in C would allow me to populate the prhs1 array such that
I could subsequently call the mexCallMATLAB function?

Subject: mexCallMATLAB populating inputs

From: James Tursa

Date: 20 Aug, 2009 00:39:19

Message: 4 of 16

John Harrold <john.m.harrold@gmail.com> wrote in message <494fa8a2-72c1-4c6a-a599-69269928edd2@r27g2000vbn.googlegroups.com>...
>
> What syntax in C would allow me to populate the prhs1 array such that
> I could subsequently call the mexCallMATLAB function?

  double a[] = {1, 2, 3, 4};
  double *pr;
  mxArray *plhs1[1], *prhs1;
  int i;
  prhs1 = mxCreateDoubleMatrix(4, 1, mxREAL);
  pr = mxGetPr(prhs1);
  for( i=0; i<3; i++ ) {
      pr[i] = a[i];
  }
  mexCallMATLAB(1, plhs1, 1, &prhs1, "mymfile");

For readability, you might consider making plhs1 and prhs1 either both arrays or both pointers. That way using them for arguments in mexCallMATLAB would look the same instead of different as I have them above.

James Tursa

Subject: mexCallMATLAB populating inputs

From: John Harrold

Date: 20 Aug, 2009 01:03:22

Message: 5 of 16

On Aug 19, 8:39 pm, "James Tursa"
<aclassyguy_with_a_k_not_...@hotmail.com> wrote:
> John Harrold <john.m.harr...@gmail.com> wrote in message <494fa8a2-72c1-4c6a-a599-69269928e...@r27g2000vbn.googlegroups.com>...
>
> > What syntax in C would allow me to populate the prhs1 array such that
> > I could subsequently call the mexCallMATLAB function?
>
>   double a[] = {1, 2, 3, 4};
>   double *pr;
>   mxArray *plhs1[1], *prhs1;
>   int i;
>   prhs1 = mxCreateDoubleMatrix(4, 1, mxREAL);
>   pr = mxGetPr(prhs1);

I think this is what I couldn't figure out.


> For readability, you might consider making plhs1 and prhs1 either both arrays or both pointers. That way using them for arguments in mexCallMATLAB would look the same instead of different as I have them above.

Yeah, you're right.

Thanks for your help this is exactly what I needed.

Subject: mexCallMATLAB populating inputs

From: oruganti murthy

Date: 20 Aug, 2009 09:34:04

Message: 6 of 16

Dear James,
I tried your recommendation and I am getting this strange result. Why?

with regards,
ramana
**************************
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    double a[] = {1, 2, 3, 4};
    double *pr;
    mxArray *prhs1,*output[1];
    int i;
    prhs1 = mxCreateDoubleMatrix(4, 1, mxREAL);
    pr = mxGetPr(prhs1);
    for( i=0; i<3; i++ ) {
        pr[i] = a[i];
    }
    mexCallMATLAB(1, output, 1, &prhs1, "mymfile");
    plhs[0] = output[0];
}
****************************
function [output]=mymfile(input)
output = input(1)+2*input(2)-input(3)^3 + input(4);
*****************************
>> mex demo2.c
>> b=demo2()

b =

   -22

>> mymfile([1 2 3 4])

ans =

   -18

Subject: mexCallMATLAB populating inputs

From: John Harrold

Date: 20 Aug, 2009 12:21:01

Message: 7 of 16

On Aug 20, 5:34 am, "oruganti murthy" <omur...@yahoo.com> wrote:
> Dear James,
> I tried your recommendation and I am getting this strange result. Why?


> >> mex demo2.c
> >> b=demo2()
>
> b =
>
>    -22
>
> >> mymfile([1 2 3 4])
>
> ans =
>
>    -18

Use <=3 in the for loop instead of strictly <3.

Subject: mexCallMATLAB populating inputs

From: James Tursa

Date: 20 Aug, 2009 16:30:20

Message: 8 of 16

John Harrold <john.m.harrold@gmail.com> wrote in message <709f10a0-f1f0-4d97-b564-bcc3acee355f@l35g2000vba.googlegroups.com>...
> On Aug 20, 5:34?am, "oruganti murthy" <omur...@yahoo.com> wrote:
> > Dear James,
> > I tried your recommendation and I am getting this strange result. Why?
>
>
> > >> mex demo2.c
> > >> b=demo2()
> >
> > b =
> >
> > ? ?-22
> >
> > >> mymfile([1 2 3 4])
> >
> > ans =
> >
> > ? ?-18
>
> Use <=3 in the for loop instead of strictly <3.

Oops ... sorry about that.

James Tursa

Subject: mexCallMATLAB populating inputs

From: oruganti murthy

Date: 21 Aug, 2009 00:29:03

Message: 9 of 16

Thank you all very much folks.

with regards,
ramana

Subject: mexCallMATLAB populating inputs

From: Ramana Murthy

Date: 28 Aug, 2009 03:49:01

Message: 10 of 16

Hi James,

One small doubt.
Suppose I call interp1.m (matlab in-built function) in mex file.
Will it's Execution time be less than what if it were to be called in a matlab script file?
with same data, of course.

I have one application (m file) where I am calling some 256x256x3 times the interp1.m function. If I send the data into mex routine and call interp1.m from mex file, compute and send the results back to m file, will it take less time?

with regards,
ramana

Subject: mexCallMATLAB populating inputs

From: James Tursa

Date: 28 Aug, 2009 06:10:03

Message: 11 of 16

"Ramana murthy" <omurthy@yahoo.com> wrote in message <h77k3d$bs4$1@fred.mathworks.com>...
> Hi James,
>
> One small doubt.
> Suppose I call interp1.m (matlab in-built function) in mex file.
> Will it's Execution time be less than what if it were to be called in a matlab script file?
> with same data, of course.
>
> I have one application (m file) where I am calling some 256x256x3 times the interp1.m function. If I send the data into mex routine and call interp1.m from mex file, compute and send the results back to m file, will it take less time?
>
> with regards,
> ramana

No. Passing the variable to a mex file and then calling interp1 with mexCallMATLAB is going to take the same amount of time as calling it directly from a script file ... you are calling the exact same function in either case. In general, any m-file called from a mex routine with mexCallMATLAB is going to take at least as long as calling the same m-file from a script file. In some cases it can even take longer, because an m-file called from a script is allowed to return a shared data copy, whereas the same m-file called from a mex routine with mexCallMATLAB is prevented from returning a shared data copy ... so extra work may have to be done for the same call from a mex routine. I suspect MATLAB enforces this for mexCallMATLAB to make sure there is no unintended data corruption downstream in the mex routine.

Situations where mex files shine is when there are a huge number of intermediate variables (matrix slices, etc.) that create a lot of overhead when coded in MATLAB, but all this overhead can potentially be saved by recoding the same algorithm in a mex routine. If the overhead is significant, the mex routine can significantly outperform the m-code.

James Tursa

Subject: mexCallMATLAB populating inputs

From: Ramana Murthy

Date: 28 Aug, 2009 09:01:03

Message: 12 of 16

That was helpful James.
Thank you very much,

with regards,
ramana

Subject: mexCallMATLAB populating inputs

From: sakshi j

Date: 23 Jun, 2010 13:13:04

Message: 13 of 16

Hi,
I'm trying to run the following piece of code:
void mexFunction(
int nlhs,
mxArray *[],
int nrhs,
const mxArray *prhs[]
)
{

   
  using namespace libbase;
  
  
  vector <double> y;


  
double *xValues;

int i,j;



mxArray *value;





xValues = mxGetPr(prhs[0]);

 int rowLen = mxGetN(prhs[0]);
 int colLen = mxGetM(prhs[0]);


y.init(2);
y=0;

y(1)=3;
y(2)=3;



printf("the rowlength is=%d",rowLen);

value = mxDuplicateArray(prhs[0]);





double neigh[] = {3,3};
  double *pr;
mxArray *p;
  
  
 

int nlhs1, nrhs1;
mxArray *plhs1[2], *prhs1[2];


nlhs1 = 2;
nrhs1 = 2;
prhs1[0]=value;


prhs1[1] = mxCreateCellMatrix(2, 1);
  pr = mxGetPr(prhs1[1]);
  for( i=0; i<=1; i++ ) {
      pr[i] = neigh[i];
  }

mxSetCell(*p,1, );
mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"wiener2");


return;

}

i first run the following:
a = im2double(imread('stego.tif'))'

Then i compile my mex file.
And I recieve the following output with this error:
the rowlength is=256the value of neigh is=0.000000the value of neigh is=3.000000??? Error using ==> ones
Size vector must be a row vector with real elements.

Error in ==> wiener2 at 55
localMean = filter2(ones(nhood), g) / prod(nhood);
 

Can anybody help me in this regard?

Subject: mexCallMATLAB populating inputs

From: sakshi j

Date: 23 Jun, 2010 13:13:05

Message: 14 of 16

Hi,
I'm trying to run the following piece of code:
void mexFunction(
int nlhs,
mxArray *[],
int nrhs,
const mxArray *prhs[]
)
{

   
  using namespace libbase;
  
  
  vector <double> y;


  
double *xValues;

int i,j;



mxArray *value;





xValues = mxGetPr(prhs[0]);

 int rowLen = mxGetN(prhs[0]);
 int colLen = mxGetM(prhs[0]);


y.init(2);
y=0;

y(1)=3;
y(2)=3;



printf("the rowlength is=%d",rowLen);

value = mxDuplicateArray(prhs[0]);





double neigh[] = {3,3};
  double *pr;
mxArray *p;
  
  
 

int nlhs1, nrhs1;
mxArray *plhs1[2], *prhs1[2];


nlhs1 = 2;
nrhs1 = 2;
prhs1[0]=value;


prhs1[1] = mxCreateCellMatrix(2, 1);
  pr = mxGetPr(prhs1[1]);
  for( i=0; i<=1; i++ ) {
      pr[i] = neigh[i];
  }

mxSetCell(*p,1, );
mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"wiener2");


return;

}

i first run the following:
a = im2double(imread('stego.tif'))'

Then i compile my mex file.
And I recieve the following output with this error:
the rowlength is=256the value of neigh is=0.000000the value of neigh is=3.000000??? Error using ==> ones
Size vector must be a row vector with real elements.

Error in ==> wiener2 at 55
localMean = filter2(ones(nhood), g) / prod(nhood);
 

Can anybody help me in this regard?

Subject: mexCallMATLAB populating inputs

From: sakshi j

Date: 23 Jun, 2010 13:16:04

Message: 15 of 16

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <h77sbr$p8s$1@fred.mathworks.com>...
> "Ramana murthy" <omurthy@yahoo.com> wrote in message <h77k3d$bs4$1@fred.mathworks.com>...
> > Hi James,
> >
> > One small doubt.
> > Suppose I call interp1.m (matlab in-built function) in mex file.
> > Will it's Execution time be less than what if it were to be called in a matlab script file?
> > with same data, of course.
> >
> > I have one application (m file) where I am calling some 256x256x3 times the interp1.m function. If I send the data into mex routine and call interp1.m from mex file, compute and send the results back to m file, will it take less time?
> >
> > with regards,
> > ramana
>
> No. Passing the variable to a mex file and then calling interp1 with mexCallMATLAB is going to take the same amount of time as calling it directly from a script file ... you are calling the exact same function in either case. In general, any m-file called from a mex routine with mexCallMATLAB is going to take at least as long as calling the same m-file from a script file. In some cases it can even take longer, because an m-file called from a script is allowed to return a shared data copy, whereas the same m-file called from a mex routine with mexCallMATLAB is prevented from returning a shared data copy ... so extra work may have to be done for the same call from a mex routine. I suspect MATLAB enforces this for mexCallMATLAB to make sure there is no unintended data corruption downstream in the mex routine.
>
> Situations where mex files shine is when there are a huge number of intermediate variables (matrix slices, etc.) that create a lot of overhead when coded in MATLAB, but all this overhead can potentially be saved by recoding the same algorithm in a mex routine. If the overhead is significant, the mex routine can significantly outperform the m-code.
>
> James Tursa

Hi,
I'm trying to run the following piece of code:
void mexFunction(
int nlhs,
mxArray *[],
int nrhs,
const mxArray *prhs[]
)
{

   
  using namespace libbase;
  
  
  vector <double> y;


  
double *xValues;

int i,j;



mxArray *value;





xValues = mxGetPr(prhs[0]);

 int rowLen = mxGetN(prhs[0]);
 int colLen = mxGetM(prhs[0]);


y.init(2);
y=0;

y(1)=3;
y(2)=3;



printf("the rowlength is=%d",rowLen);

value = mxDuplicateArray(prhs[0]);





double neigh[] = {3,3};
  double *pr;
mxArray *p;
  
  
 

int nlhs1, nrhs1;
mxArray *plhs1[2], *prhs1[2];


nlhs1 = 2;
nrhs1 = 2;
prhs1[0]=value;


prhs1[1] = mxCreateCellMatrix(2, 1);
  pr = mxGetPr(prhs1[1]);
  for( i=0; i<=1; i++ ) {
      pr[i] = neigh[i];
  }

mxSetCell(*p,1, );
mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"wiener2");


return;

}

i first run the following:
a = im2double(imread('stego.tif'))'

Then i compile my mex file.
And I recieve the following output with this error:
the rowlength is=256the value of neigh is=0.000000the value of neigh is=3.000000??? Error using ==> ones
Size vector must be a row vector with real elements.

Error in ==> wiener2 at 55
localMean = filter2(ones(nhood), g) / prod(nhood);
 

Can anybody help me in this regard?

Subject: mexCallMATLAB populating inputs

From: sakshi j

Date: 23 Jun, 2010 13:28:04

Message: 16 of 16

"sakshi j" <sakshi399@yahoo.co.in> wrote in message <hvt191$o3f$1@fred.mathworks.com>...
> Hi,
> I'm trying to run the following piece of code:
> void mexFunction(
> int nlhs,
> mxArray *[],
> int nrhs,
> const mxArray *prhs[]
> )
> {
>
>
> using namespace libbase;
>
>
> vector <double> y;
>
>
>
> double *xValues;
>
> int i,j;
>
>
>
> mxArray *value;
>
>
>
>
>
> xValues = mxGetPr(prhs[0]);
>
> int rowLen = mxGetN(prhs[0]);
> int colLen = mxGetM(prhs[0]);
>
>
> y.init(2);
> y=0;
>
> y(1)=3;
> y(2)=3;
>
>
>
> printf("the rowlength is=%d",rowLen);
>
> value = mxDuplicateArray(prhs[0]);
>
>
>
>
>
> double neigh[] = {3,3};
> double *pr;
> mxArray *p;
>
>
>
>
> int nlhs1, nrhs1;
> mxArray *plhs1[2], *prhs1[2];
>
>
> nlhs1 = 2;
> nrhs1 = 2;
> prhs1[0]=value;
>
>
> prhs1[1] = mxCreateCellMatrix(2, 1);
> pr = mxGetPr(prhs1[1]);
> for( i=0; i<=1; i++ ) {
> pr[i] = neigh[i];
> }
>
> mxSetCell(*p,1, );
> mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"wiener2");
>
>
> return;
>
> }
>
> i first run the following:
> a = im2double(imread('stego.tif'))'
>
> Then i compile my mex file.
> And I recieve the following output with this error:
> the rowlength is=256the value of neigh is=0.000000the value of neigh is=3.000000??? Error using ==> ones
> Size vector must be a row vector with real elements.
>
> Error in ==> wiener2 at 55
> localMean = filter2(ones(nhood), g) / prod(nhood);
>
>
> Can anybody help me in this regard?


SORRY...correction(this is the erraneous code):
Hi,
I'm trying to run the following piece of code:
void mexFunction(
int nlhs,
mxArray *[],
int nrhs,
const mxArray *prhs[]
)
{

   
  using namespace libbase;
  
  
  vector <double> y;


  
double *xValues;

int i,j;



mxArray *value;





xValues = mxGetPr(prhs[0]);

 int rowLen = mxGetN(prhs[0]);
 int colLen = mxGetM(prhs[0]);


y.init(2);
y=0;

y(1)=3;
y(2)=3;



printf("the rowlength is=%d",rowLen);

value = mxDuplicateArray(prhs[0]);





double neigh[] = {3,3};
  double *pr;
mxArray *p;
  
  
 

int nlhs1, nrhs1;
mxArray *plhs1[2], *prhs1[2];


nlhs1 = 2;
nrhs1 = 2;
prhs1[0]=value;


prhs1[1] = mxCreateDoubleMatrix(2, 1);
  pr = mxGetPr(prhs1[1]);
  for( i=0; i<=1; i++ ) {
      pr[i] = neigh[i];
  }


mexCallMATLAB(nlhs1,plhs1,nrhs1,prhs1,"wiener2");


return;

}

i first run the following:
a = im2double(imread('stego.tif'))'

Then i compile my mex file.
And I recieve the following output with this error:
the rowlength is=256the value of neigh is=0.000000the value of neigh is=3.000000??? Error using ==> ones
Size vector must be a row vector with real elements.

Error in ==> wiener2 at 55
localMean = filter2(ones(nhood), g) / prod(nhood);
 

Can anybody help me in this regard?

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
mexcallmatlab sakshi j 23 Jun, 2010 09:14:16
c Sprinceana 9 Sep, 2009 02:38:41
mex Sprinceana 9 Sep, 2009 02:38:41
rssFeed for this Thread

Contact us at files@mathworks.com