Main Content

mdlUpdate

Update a block's states

Required

No

语言

C, C++

Syntax

#define MDL_UPDATEvoid mdlUpdate(SimStruct *S, int_T tid)

Arguments

S

SimStruct representing an S-Function block.

tid

Task ID.

Description

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 doesnothave 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 itsmdlUpdateroutine called and it does not satisfy either of the above two conditions, specify that it has a discrete state, using thessSetNumDiscStatesmacro in themdlInitializeSizesfunction.

In C MEX S-functions, thetid(task ID) argument specifies the task running when themdlOutputsroutine is invoked. You can use this argument in themdlUpdateroutine of a multirate S-Function block to encapsulate task-specific blocks of code (seeMultirate S-Function Blocks).

Use theUNUSED_ARGmacro if your C MEX S-function does not contain task-specific blocks of code to indicate that thetidinput argument is required but not used in the body of the callback. To do this, insert the line

UNUSED_ARG(tid)

after the declarations inmdlUpdate.

Note

If you haveSimulink Coder™, 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, int_T tid) { /* Add mdlUpdate code here */ } #endif

Thedefinestatement makes themdlUpdatemethod available only to a MATLAB®MEX file. If the S-function is not inlined,Simulink Codercannot use this method, resulting in link or run-time errors.

Example

For an example that uses this function to update discrete states, seedsfunc.c. For an example that uses this function to update the transfer function coefficients of a time-varying continuous transfer function, seestvctf.c.

Introduced before R2006a