Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Passing function arguments to Matlab via Matlab Engine

Subject: Passing function arguments to Matlab via Matlab Engine

From: mayur

Date: 29 Jan, 2013 10:39:08

Message: 1 of 4

If someone can help me here , it will be greatly appreciated ...

I was able to set up the Matlab Engine Call via C properly without any issues.. , done it via a C function calll .. something like this ... :
//////////////////////////////////////////////////
#include <math.h>
....
int matlab_call(double Amp)

{
Engine *ep;
mxArray *A = NULL, *result = NULL;
if (!(ep = engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
            } ...
}
//////////////////////////////////////////////////

Now i want to send in the input argument Amp to Matlab as a variable...

I did the following which i am not sure if its correct .. please help..
///////////////////////////////////////////////
A = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); // created a numeric matrix of size 1 and of type double
memcpy(&A,&Amp,sizeof(double)); // copied over the memory from the input argument over to the mxArray..
//////////////////////////////////////////////

After this i used the engPutVariable as follows :
//////////////////////////////////////////////
  engPutVariable(ep, "A", A) ;
//////////////////////////////////////////////

But then when i tried and retrieved the data via engGetVariable i get a "Can't read file stdio" error....

//////////////////////////////////////////////
((result = engGetVariable(ep,"A")) == NULL); // gives a stdio error
//////////////////////////////////////////////

Can someone has been able to send over C variables to Matlab .. please do help

Subject: Passing function arguments to Matlab via Matlab Engine

From: James Tursa

Date: 29 Jan, 2013 18:26:08

Message: 2 of 4

"mayur" wrote in message <ke88sc$q9$1@newscl01ah.mathworks.com>...
>
> I did the following which i am not sure if its correct .. please help..
>
> A = mxCreateNumericMatrix(1,1,mxDOUBLE_CLASS,mxREAL); // created a numeric matrix of size 1 and of type double
> memcpy(&A,&Amp,sizeof(double)); // copied over the memory from the input argument over to the mxArray..

The above line is an error. What you have done is copy the value of Amp into the first 8 bytes of the mxArray structure of A. What you *want* to do is copy the value into the *data* area of A instead. E.g.,

*mxGetPr(A) = Amp;

or using memcpy:

memcpy(mxGetPr(A),&Amp,sizeof(double));


James Tursa

Subject: Passing function arguments to Matlab via Matlab Engine

From: mayur

Date: 30 Jan, 2013 16:19:08

Message: 3 of 4

Thanks James

Let me try out this ... I have seen some of your earlier posts for the other posts and had some how thought that you could be one to answer ... thanks again ..

Subject: Passing function arguments to Matlab via Matlab Engine

From: mayur

Date: 1 Feb, 2013 09:13:06

Message: 4 of 4

This works .. thanks James

Tags for this Thread

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.

Contact us