| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
No
#define MDL_UPDATE
void mdlUpdate(SimStruct *S, int_T tid)
Update(s)
Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 M-File S-Function block.
The Simulink engine invokes this optional method at each major simulation time step. The method should compute the S-function's states at the current time step and store the states in the S-function's state vector. The method can also perform any other tasks that the S-function needs to perform at each major time step.
Use this code if your S-function has one or more discrete states or does not have direct feedthrough.
The reason for this is that most S-functions that do not have discrete states but do have direct feedthrough do not have update functions. Therefore, the engine is able to eliminate the need for the extra call in these circumstances.
If your C MEX S-function needs to have its mdlUpdate routine called and it does not satisfy either of the above two conditions, specify that it has a discrete state, using the ssSetNumDiscStates macro in the mdlInitializeSizes function.
In C MEX S-functions, the tid (task ID) argument specifies the task running when the mdlOutputs routine is invoked. You can use this argument in the mdlUpdate routine of a multirate S-Function block to encapsulate task-specific blocks of code (see Multirate S-Function Blocks).
Use the UNUSED_ARG macro if your C MEX S-function does not contain task-specific blocks of code to indicate that the tid input argument is required but not used in the body of the callback. To do this, insert the line
UNUSED_ARG(tid)
after the declarations in mdlUpdate.
In Level-2 M-file S-functions, use the run-time object method IsSampleHit to determine if the current simulation time is one at which a task handled by this block is active. For port-based sample times, use the IsSampleHit property of the run-time object's InputPort or OutputPort to determine if the port produces outputs or accepts inputs at the current simulation time step.
Note When generating code for a noninlined C MEX S-function that contains this method, make sure the method is not wrapped in a #if defined(MATLAB_MEX_FILE) statement. For example: #define MDL_UPDATE
#if defined(MDL_UPDATE) && defined(MATLAB_MEX_FILE)
static void mdlUpdate(SimStruct *S)
{
/* Add mdlUpdate code here *
}
#endif The define statement makes the mdlUpdate method available only to a MATLAB MEX-file. If the S-function is not inlined, the Real-Time Workshop product cannot use this method, resulting in link or run-time errors. |
For an example that uses this function to update discrete states, see matlabroot/toolbox/simulink/simdemos/simfeatures/src/dsfunc.c. For an example that uses this function to update the transfer function coefficients of a time-varying continuous transfer function, see matlabroot/toolbox/simulink/simdemos/simfeatures/src/stvctf.c.
For an example that uses this function to update discrete states, see matlabroot/toolbox/simulink/simdemos/simfeatures/msfcn_unit_delay.m.
C, C++, M
mdlDerivatives, ssGetContStates, ssGetDiscStates
![]() | mdlTerminate | mdlZeroCrossings | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |