MATLAB: Call M-script in C/C++ Sfunction

s-functionsimulink

I have a S-Function written in C. I want to call a m-script during from this S-Function. How is this possible?
Thanke

Best Answer

I understand that you have written a S-Function in C and you want to call a matlab script from that function. I assume that you are using MATLAB R2016b.
You can do the above by using 'mexEvalString('Name_of_Script')' function in mdlOutputs method of S-Function. To illustrate, I am using timestwo example available at the following link http://in.mathworks.com/help/simulink/sfg/example-of-a-basic-c-mex-s-function.html and a dummy script plotline.m which plots a line. I am calling this MATLAB Script from timestwo S-function.
plotLine.m looks as below:
x = [1 2 3];
y = [1 2 3];
plot(x,y);
mdlOutputs method in timestwo.c looks as below:
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
for (i=0; i<width; i++) {
*y++ = 2.0 *(*uPtrs[i]);
}
mexEvalString("plotLine");
}
Now use mex timestwo.c to compile this S-Function. Run the S-function and notice that it also executes the script now.
I hope that this helps.