Thread Subject: mexCallMATLAB populating inputs

Subject: mexCallMATLAB populating inputs

From: John Harrold

Date: 19 Aug, 2009 20:27:00

Message: 1 of 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

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 12

"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 12

That was helpful James.
Thank you very much,

with regards,
ramana

Tags for this Thread

Everyone's Tags:

mex, c

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 Sprinceana 9 Sep, 2009 02:38:41
c Sprinceana 9 Sep, 2009 02:38:41
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com