필터 지우기
필터 지우기

Can C code user function be used in Simulink S-Function

조회 수: 2 (최근 30일)
Brian Tremaine
Brian Tremaine 2020년 12월 9일
댓글: Walter Roberson 2020년 12월 9일
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

채택된 답변

Walter Roberson
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
Brian Tremaine
Brian Tremaine 2020년 12월 9일
My S-Function is apparently a Level-1 and it is true it is implemented in C. What I am trying to do is the following. My mdlOutputs procedure processes data involving the current states and the input values. The computation is on the order of 300+ lines of code. I would like to process the data using a reference to a struct, for example: ComputeXY( sv), where sv is a pointer to my struct. The procedure ComputeXY is quite large and can be called in several locations in mdlOutputs, (from C switch statements), but it is only called once each execution of mdlOutputs. I have a second procedure, call it ComputeCntl(sv), that is also quite large that I would like to call before I update the output values and return from mdlOutputs.
Ordinarily for other projects my computation in mdlOutputs is only a small number of lines of code and I don't have a need to do the computation in a separate C function.
Thanks,
Walter Roberson
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 CenterFile Exchange에서 Block and Blockset Authoring에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by