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

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 9 Dec 2020
Edited: Walter Roberson on 9 Dec 2020
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 Comments
Brian Tremaine
Brian Tremaine on 9 Dec 2020
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 on 9 Dec 2020
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 .)

Sign in to comment.

More Answers (0)

Categories

Find more on Block and Blockset Authoring in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!