简体   繁体   中英

How do I call a matlab variable from an S function?

I am working on an S function in simulink. There are some variables in the MATLAB workspace available. I want to call them.

So in MATLAB:

a=3;

and in the S function (written in C/C++):

double a = CallFromMATLABWorkSpace(a);  //Something like this.

How do I do this? There is something like mexCallMATLAB but it is not clear how I should use this in this situation.

To get data from a workspace use the function mexGetVariable .

However, this is a somewhat unusual thing to do. Why isn't the data being passed as a parameter to the S-Function?

From what I can see in the documentation for mexCallMATLAB , as well as interoping with C++ source code , it would look something like the following:

Let's say you have a MatLab function MyDoubleFunction that takes a single scalar double value and returns a scalar double value. You would do the following if you wanted to pass the function a value of 4.0 and see what the answer is:

//setup the input args
mxArray* input_args[1] = {mxCreateDoubleScalar(4.0)};
mxArray** output_args; //will be allocated during call to mexCallMATLAB

//make the call to the Matlab function
if (mexCallMATLAB( 1 /* number of output arguments */,
                   output_args,
                   1 /* number of input arguments */,
                   &input_args,
                   "MyDoubleFunction"))
{
    //error if we get to this code block since it returned a non-zero value
}

//inspect the output arguments
double answer = mxGetScalar(*output_args);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM