Can C code user function be used in Simulink S-Function
1 回表示 (過去 30 日間)
古いコメントを表示
I have a large simulation in Simulink that uses S-functions written in c-code. For readability I would like to define a user function and call it from with an S method.
for example, see snippet below. Is this allowable in an S-Function? Is there anything I should be aware of or look out for?
static void UserFnc( real_T u, real_T v, real_T * x); // delcare
....
....
static void mdlOutputs(SimStruct *S, int_T tid)
{
..........
UserFnc( real_T u, real_T v, real_T * x); // use here
.........
.........
UserFnc( real_T u, real_T v, real_T * x); // and use here
.........
}
........
........
static void UserFnc( real_T u, real_T v, real_T * x) // define here
{
// * User code here */
}
/*=============================*
* Required S-function trailer *
*=============================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
0 件のコメント
採用された回答
Walter Roberson
2020 年 12 月 9 日
編集済み: Walter Roberson
2020 年 12 月 9 日
Yes, see coder.ceval()
On the other hand, your code structure looks to me to be more like the implementation of a Level-1 S Function, and the above is not the way to invoke a complete S-function.
4 件のコメント
Walter Roberson
2020 年 12 月 9 日
MATLAB has no problem with you creating separately linked C files if appropriate, or putting everything into one file if appropriate.
But if you are working on "current states" then you should be looking into whether you should be switching to Level 2 S Function. The major difference between Level 1 and Level 2 is the treatment of state vectors (and the fact that Level 2 allows you to access and control other blocks .)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Block and Blockset Authoring についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!