Can C code user function be used in Simulink S-Function
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Brian Tremaine
el 9 de Dic. de 2020
Comentada: Walter Roberson
el 9 de Dic. de 2020
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 comentarios
Respuesta aceptada
Walter Roberson
el 9 de Dic. de 2020
Editada: Walter Roberson
el 9 de Dic. de 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 comentarios
Walter Roberson
el 9 de Dic. de 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 .)
Más respuestas (0)
Ver también
Categorías
Más información sobre Block and Blockset Authoring en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!