Documentation Center

  • Trial Software
  • Product Updates

S-Function Compliance with the SimState

SimState Compliance Specification for Level-2 MATLAB S-Functions

In order for a Level-2 MATLAB® S-function to work with the SimState feature, you must specify the simStateCompliance of the block using the method,

block.simStateCompliance = setting

where the permissible setting values are:

SettingResult
'UnknownSimState'This default setting instructs Simulink® to use the DefaultSimState to save and restore the SimState and issues a warning.
'DefaultSimState'This setting instructs Simulink to treat the S-function like a built-in block when saving and restoring the SimState.
'HasNoSimState'This setting informs Simulink that the S-function does not have any simulation state. With this setting, no state information is saved for the block. This setting is primarily useful for "sink" blocks (i.e., blocks with no output ports) that use PWorks or DWorks to store handles to files or figure windows.
'CustomSimState'This setting informs Simulink that the S-function has custom GetSimState and SetSimState methods.
'DisallowSimState'This setting informs Simulink that the S-function does not allow saving or restoring its simulation state. Simulink reports an error if you save and restore the SimState of the model that contains this S-function.

For an S-function with custom methods ('CustomSimState'), you can use the following statements to respectively get and set the SimState:

function outSS = GetSimState(block)
function SetSimState(block, inSS)

For an example of how to implement these custom methods, see msfcn_varpulse.mmsfcn_varpulse.m.

SimState Compliance Specification for C-MEX S-Functions

As with the MATLAB S-function, your C-MEX S-function code must inform Simulink of the S-function compliance with the SimState feature. You can accomplish this task by using the S-function API, ssSetSimStateCompliance.

In most cases, you must add only the following line:

ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE).

The options are as follows:

SettingResult
SIM_STATE_COMPLIANCE_UNKNOWNThis is the default setting for all S-functions. For S-functions that do not use PWorks, Simulink saves and restores the default simulation state (see next option) and issues a warning to inform the user of this assumption. On the other hand, Simulink reports an error during the save and restore if it encounters an S-function that uses PWorks.
USE_DEFAULT_SIM_STATEThis setting instructs Simulink to treat the S-function like a built-in block when saving and restoring the SimState.
HAS_NO_SIM_STATEThis setting informs Simulink that the S-function does not have any simulation state. With this setting, no state information is saved for this block. This setting is primarily useful for "sink" blocks (i.e., blocks with no output ports) that use PWorks or DWorks to store handles to files or figure windows.
DISALLOW_SIM_STATEThis setting informs Simulink that the S-function does not allow the saving or restoring of its simulation state. Simulink reports an error if you save and restore the SimState of the model that contains this S-function.
USE_CUSTOM_SIM_STATEThis setting informs Simulink that the S-function has mdlGetSimState and mdlSetSimState methods.

For S-functions that use PWork vectors or static variables to hold data that Simulink updates during simulation, the S-function must use the custom mdlGetSimState and mdlSetSimState methods. The following statements demonstrate the proper format.

mxArray* mdlGetSimState(SimStruct* S)
void mdlSetSimState(SimStruct* S, const mxArray* inSS)

For an example of how to implement these methods, see sfun_simstate.csfun_simstate.c.

Was this topic helpful?