| Writing S-Functions | ![]() |
Outputs Pane
Use the Outputs pane to enter code that computes the outputs of the S-function at each simulation time step.
The Outputs pane contains the following fields.
Code for the mdlOutputs function
Code that computes the output of the S-function at each simulation time step (or sample time hit, in the case of a discrete S-function). When generating the source code for the S-function, the S-Function Builder inserts the code in this field in a wrapper function of the form
void sfun_Outputs_wrapper(const real_T *u, real_T *y, const real_T *xD, /* optional */ const real_T *xC, /* optional */ 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 mdlOutputs callback method that it generates for your S-function. Simulink invokes the mdlOutputs method at each simulation time step (or sample time step in the case of a discrete S-function) to compute the S-function's output. The S-function's mdlOutputs method in turn invokes the wrapper function containing your output code. Your output code then actually computes and returns the S-function's output.
The mdlOutputs method passes some or all of the following arguments to the outputs wrapper function.
| Argument |
Description |
u |
Pointer to an array containing the inputs to the S-function. The width of the array is the same as the input width you specified on the Initialization pane. If you specified -1 as the input width, the width of the array is specified by the wrapper function's u_width argument (see below). |
y |
Pointer to an array containing the output of the S-function.The width of the array is the same as the output width you specified on the Initialization pane. If you specified -1 as the output width, the width of the array is specified by the wrapper function's y_width argument (see below). Use this array to pass the outputs that your code computes back to Simulink. |
xD |
Pointer to an array containing the discrete states of the S-function. This argument appears only if you specified discrete states on the Initialization pane. At the first simulation time step, the discrete states have the initial values that you specified on the Initialization pane. At subsequent sample-time steps, the states are obtained from the values that the S-function computes at the preceding time step (see Discrete Update Pane for more information). |
xC |
Pointer to an array containing the continuous states of the S-function. This argument appears only if you specified continuous states on the Initialization pane. At the first simulation time step, the continuous states have the initial values that you specified on the Initialization pane. At subsequent time steps, the states are obtained by numerically integrating the derivatives of the states at the preceding time step (see Continuous Derivatives Pane for more information). |
param0, p_width0, param1, p_width1, ... paramN, p_widthN |
param0, param1, paramN are pointers to arrays containing the S-function's parameters, where N is the number of parameters specified on the Initialization pane. p_width0, p_width1, p_widthN are the widths of the parameter arrays. If a parameter is a matrix, the width equals the product of the dimensions of the arrays. For example, the width of a a 3-by-2 matrix parameter is 6. These arguments appear only if you specify parameters on the Initialization pane. |
y_width |
Width of the array containing the S-function's outputs. This argument appears in the generated code only if you specified -1 as the width of the S-function's output. If the output is a matrix, y_width is the product of the dimensions of the matrix. |
u_width |
Width of the array containing the S-function's inputs. This argument appears in the generated code only if you specified -1 as the width of the S-function's input. If the input is a matrix, u_width is the product of the dimensions of the matrix. |
These arguments permit you to compute the output of the block as a function of its inputs and, optionally, its states and parameters. The code that you enter in this field can invoke external functions declared in the header files or external declarations on the Libraries pane. This allows you to use existing code to compute the outputs of the S-function.
Inputs are needed in the output function
Selected if the current values of the S-function's inputs are used to compute its outputs. Simulink uses this information to detect algebraic loops created by directly or indirectly connecting the S-function's output to its input.
| Libraries Pane | Continuous Derivatives Pane | ![]() |
© 1994-2005 The MathWorks, Inc.