C

Asked • 05/28/19

Simulink: How to use a local variable in a level 2 s-function?

I have written a Simulink S-function (Level 2) in C. The resulting block has one output and one parameter. This parameter is stored in a variable, which is defined at file scope, right after setting up the block: #define NUM_PARAMS 1 #define NUM_INPORTS 0 #define NUM_OUTPORTS 1 unsigned short int MASK_INDEX; I assign it within mdlInitializeSizes, and do some operations on its value: static void mdlInitializeSizes(SimStruct *S) {    // Check Parameters   ssSetNumSFcnParams(S, NUM_PARAMS);   if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {     return;   }   MASK_INDEX = *mxGetPr(ssGetSFcnParam(S, 0)); (...) operations } My problem is, that the variable MASK_INDEX seems to be global, and shared among all blocks of the same type. Therefore, it holds the same value for all blocks. As a workaround, I reload it every time, and re-do the operations, for example: static void mdlOutputs(SimStruct *S, int_T tid) {   MASK_INDEX = *mxGetPr(ssGetSFcnParam(S, 0)); (...) operations } How can I get a true "local variable", so that I don't have to repeat all this every time?

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.