Can C code user function be used in Simulink S-Function
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Brian Tremaine
le 9 Déc 2020
Commenté : Walter Roberson
le 9 Déc 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 commentaires
Réponse acceptée
Walter Roberson
le 9 Déc 2020
Modifié(e) : Walter Roberson
le 9 Déc 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 commentaires
Walter Roberson
le 9 Déc 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 .)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Block and Blockset Authoring dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!