In the S-Function Builder block dialog, in the Output ports tab, I have set the Rows to -1 in order to have dynamically sized output. I have done the same for the Input ports. Show The input will automatically fits with the input width fetched in Simulink. What about the output ? How can I set the output port width in my C code ? I have tried the following function In Simulink, it shows the same port width(dimension) than the input's. S-Function Builder Dialog Box The tabbed panes on the S-Function Builder dialog box enable you to enter information and custom code required to tailor the S-function to a specific application. The dialog box contains the following panes. Initialization
Pane The Initialization pane allows you to specify basic features of the S-function, such as the width of its input and output ports and its sample time.
The S-Function Builder uses the information that you enter on this pane to generate the S-function's mdlInitializeSizes callback method. Simulink invokes this method during the model initialization phase of the simulation to obtain basic information about the S-function. (See How Simulink Interacts with C S-Functions for more information on the model initialization phase.) The Initialization pane contains the following fields. Number of discrete states. Number of discrete states that the S-function has. Discrete states IC. Initial conditions of the S-function's discrete states. You can enter the values as a comma-separated list or as a vector (e.g., Number of continuous states. Number of continuous states that the S-function has. Continuous states IC. Initial conditions of the S-function's continuous states. You can enter the values as a comma-separated list or as a vector (e.g., Sample mode. Sample mode of the S-function. The sample mode determines the length of the interval between the times when the S-function updates its output. You can select one of the following options:
Sample time value. Interval between updates of the S-function's outputs. This
field is enabled only if you have selected Input port width. Width of the S-function's input port. The width is the number of elements that a vector signal connected to the port must have. To permit connection of matrix (2-D) signals to the input port, specify Output port width. Width of the S-function's output port. The width is the number of elements in the vector that this S-function outputs. If the S-function outputs matrix signals, specify Number of parameters. Number of parameters that this S-function accepts. Data Properties Pane The Data Properties pane allows you to add ports and parameters to your S-function.
This pane itself contains tabbed panes that respectively display the attributes of the S-function's
The column of buttons to the left of the panes allows you to add, delete, or reorder ports or parameters, depending on the currently selected pane.
Input Ports Pane The Input Ports pane allows you to inspect and modify the properties of the S-function's input ports.
The pane comprises an editable table that lists the properties of the input ports in the order in which the ports appear on the S-function block. Each row of the table corresponds to a port. Each entry in the row displays a property of the port as follows. Port name. Name of the port. Edit this field to change the port name. Data type Lists the data type of signals accepted by the port. Click the adjacent button to display a list of supported data types. To change the port's data type, select a new type from the list. Dimensions. Lists the number of dimensions of input signals accepted by the port. To display a list of supported dimensions, click the adjacent button. To change the port's dimensionality, select a new value from the list. (Simulink signals can have at most two dimensions). Row. Specifies the size of the input signal's first (or only) dimension. Column. Specifies the size of the input signal's second dimension (only if the input port accepts 2-D signals). Complexity. Specifies whether the input port accepts real or complex-valued signals. Frame. Specifies whether this port accepts frame-based signals generated by the Digital and Signal Processing Blockset and Communications Blockset. See the documentation for these blocksets for more information. Output Ports
Pane The Output Ports pane allows you to inspect and modify the properties of the S-function's output ports.
The pane comprises an editable table that lists the properties of the output ports in the order in which the ports appear on the S-function block. Each row of the table corresponds to a port. Each entry in the row displays a property of the port as follows. Port name. Name of the port. Edit this field to change the port name. Data type Lists the data type of signals output by the port. Click the adjacent button to display a list of supported data types. To change the port's data type, select a new type from the list. Dimensions. Lists the number of dimensions of signals output by the port. To display a list of supported dimensions, click the adjacent button. To change the port's dimensionality, select a new value from the list. (Simulink signals can have at most two dimensions). Row. Specifies the size of the output signal's first (or only) dimension. Column. Specifies the size of the output signal's second dimension (only if the output port accepts 2-D signals). Complexity. Specifies whether the port outputs real or complex-valued signals. Frame. Specifies whether this port accepts frame-based signals generated by the Digital and Signal Processing Blockset and Communications Blockset. See the documentation for these blocksets for more information. Parameters Pane The Parameters pane allows you to inspect and modify the properties of the S-function's parameters.
The pane comprises an editable table that lists the properties of the S-function's parameters . Each row of the table corresponds to a port. The order in which the parameters appear corresponds to the order in which the user must specify them. Each entry in the row displays a property of the parmeter as follows. Parameter name. Name of the parameter. Edit this field to change the name. Data type Lists the data type of the parameter. Click the adjacent button to display a list of supported data types. To change the parameter's data type, select a new type from the list. Complexity. Specifies whether the parameter has real or complex values. Libraries Pane The Libraries pane allows you to specify the location of external code files referenced by custom code that you enter in other panes of the S-Function Builder dialog box.
The Libraries pane includes the following fields. Library/Object/Source files. External library, object code, and source files referenced by custom code that you enter elsewhere on the S-Function Builder dialog box. List each file on a separate line. If the file resides in the current directory, you need specify only the file's name. If the file resides in another directory, you must specify the full path of the file. Includes. Header files containing declarations of functions, variables, and macros referenced by custom code that you enter elsewhere on the S-Function Builder dialog box. Specify each file on a separate line as External function declarations. Declarations of external functions not declared in the header files listed in the Includes field. Put each declaration on a separate line. The S-Function Builder includes the specified declarations in the S-function source file that it generates. This allows S-function code that computes the S-function's states or output to invoke the external 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
where The
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. Continuous Derivatives Pane If the S-function has continuous states, use the Continuous Derivatives pane to enter code required to compute the state derivatives.
Enter code to compute the derivatives of the S-function's continuous states in the Code for the mdlDerivatives 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
where The generated S-function's
The 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
where The generated S-function's
See mdlOutputs for the meanings and usage of these arguments. Your code should use the Build Info Pane Use the Build Info pane to specify options for building the S-function MEX file.
This pane contains the following fields. Compilation diagnostics. Display diagnostic messages issued by the S-Function Builder when building the S-function. Show compile steps. Log each build step in the Compilation diagnostics field. Create a debuggable MEX-file. Include debug information in the generated MEX-file. Generate wrapper TLC. Generate a TCL file. You do not need to generate a TLC file if you do not expect the S-function ever to run in accelerated mode or be used in a model from which RTW generates code. Save code only. Do not build a MEX file from the generated source code.
What is a function in S?An S-function is a computer language description of a Simulink block written in MATLAB®, C, C++, or Fortran. C, C++, and Fortran S-functions are compiled as MEX files using the mex utility (see Build C MEX Function).
What is a Level 2 sThe Level-2 MATLAB® S-function API allows you to use the MATLAB language to create custom blocks with multiple input and output ports and capable of handling any type of signal produced by a Simulink® model, including matrix and frame signals of any data type.
What is the S block in Simulink?The S-Function block displays the name of the specified S-function and the number of input and output ports specified by the S-function. Signals connected to the inputs must have the dimensions specified by the S-function for the inputs.
What is direct feedthrough?Direct feedthrough means that the output (or the variable sample time for variable sample time blocks) is controlled directly by the value of an input port signal. Typically, an S-function input port has direct feedthrough if. The output function ( mdlOutputs ) is a function of the input u .
|