Call M-script in C/C++ Sfunction

7 ビュー (過去 30 日間)
Andreas
Andreas 2017 年 2 月 10 日
回答済み: Sonam Gupta 2017 年 2 月 14 日
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

採用された回答

Sonam Gupta
Sonam Gupta 2017 年 2 月 14 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBlock and Blockset Authoring についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by