| Writing S-Functions | ![]() |
Discrete Update Pane
If the S-function has discrete states, use the Discrete Update pane to enter code that computes at the current time step the values of the discrete states at the next time step.
Enter code to compute the values of the S-function's discrete states in the Code for the mdlUpdate function field on this pane. When generating code, the S-Function Builder takes the code in this pane and inserts it in a wrapper function of the form
void sfun_Update_wrapper(const real_T *u, const real_T *y, real_T *xD, const real_T *param0, /* optional */ int_T p_width0, /* optional */ real_T *param1,/* optional */ int_T p_width1, /* optional */ int_T y_width, /* optional */ int_T u_width) /* optional */ { /* Your code inserted here. */ }
where sfun is the name of the S-function. The S-Function Builder inserts a call to this wrapper function in the mdlUpdate callback method that it generates for the S-function. Simulink calls the mdlUpdate method at the end of each time step to obtain the values of the S-function's discrete states at the next time step (see How Simulink Interacts with C S-Functions). At the next time step, Simulink passes the updated states back to the S-function's mdlOutputs method (see Outputs Pane).
The generated S-function's mdlUpdates callback method passes the following arguments to the updates wrapper function:
u
y
xD
param0, p_width0, param1, p_width1, ... paramN, p_widthN
y_width
x-width
See mdlOutputs for the meanings and usage of these arguments. Your code should use the xD (discrete states) variable to return the values of the derivatives that it computes. The arguments allow your code to compute the discrete states as functions of the S-function's inputs, outputs, and, optionally, parameters. Your code can invoke external functions declared on the Libraries pane.
| Continuous Derivatives Pane | Build Info Pane | ![]() |
© 1994-2005 The MathWorks, Inc.