| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
This section explains how to create an S-function that accepts and/or produces frame-based signals. See Frame-Based Signals in the Working with Signals section of the Signal Processing Blockset documentation for a comprehensive discussion of the use of frame-based signals in Simulink models.
Note Simulating a model containing an S-function that accepts or produces frames requires a Signal Processing Blockset product license. |
To accept or produce frame-based signals, a C MEX S-function must perform the following tasks:
The S-function's mdlInitializeSizes callback method must set the port frame status to FRAME_YES, FRAME_NO, or FRAME_INHERITED for each of the S-function's I/O ports, using the ssSetInputPortFrameData and ssSetOutputPortFrameData functions. The frame status for a port must be set after the call to ssSetNumInputPorts and ssSetNumOutputPorts. For example, the following code in mdlInitializeSizes specifies that the first input port accepts a frame-based signal while the first output port emits a sample-based signal:
ssSetNumInputPorts(S, 1); ssSetInputPortFrameData(S, 0, FRAME_YES); ssSetNumOutputPorts(S,1); ssSetOutputPortFrameData(S, 0, FRAME_NO);
The S-function should specify the dimensions of the signals that its frame-based ports accept or produce in its mdlInitializeSizes or mdlSetInputPortDimensionInfo and mdlSetOutputPortDimensionInfo callback methods. Note that frame-based signals must be dimensioned as 2-D arrays. For example, the following code in mdlInitializeSizes specifies that the first frame-based input port is dynamically sized. This S-function must then also have an mdlSetInputPortDimensionInfo callback that sets the specific dimensions of this input port.
ssSetNumInputPorts(S, 1); ssSetInputPortFrameData(S, 0, FRAME_YES); ssSetInputPortMatrixDimensions(S, 0, DYNAMICALLY_SIZED, DYNAMICALLY_SIZED);
If the frame status of any of the S-function's input ports is inherited, the S-function should define a mdlSetInputPortFrameData callback method. The Simulink engine passes the frame status that it assigns to the port, based on frame signal propagation rules, as an argument to this callback method. The callback method should in turn use the ssSetInputPortFrameData function to set the port to the assigned status if it is acceptable or signal an error using ssSetErrorStatus if it is not. If the frame status of other ports of the S-function depend on the status inherited by one of its input ports, the callback method can also use ssSetInputPortFrameData to set the frame status of the other ports based on the status that the input port inherits. A template for the mdlSetInputPortFrameData callback is shown below.
#if defined(MATLAB_MEX_FILE)
#define MDL_SET_INPUT_PORT_FRAME_DATA
static void mdlSetInputPortFrameData(SimStruct *S,
int_T portIndex,
Frame_T frameData)
{
if(!frameData==FRAME_YES) {
ssSetErrorStatus(S, "Incorrect frame status");
return;
}
ssSetInputPortFrameData(S, portIndex, frameData); /* Sets frame status */
} /* end mdlSetInputPortFrameData */
#endifThe S-function's mdlOutputs method should include code to process the signals. The macro ssGetInputPortDimensions can be used in mdlOutputs to determine the dimensions of dynamically sized frame-based inputs, as follows:
int *dims = ssGetInputPortDimensions(S, 0); int frameSize = dims[0]; int numChannels = dims[1];
See the frame-based A/D converter S-function example (sfun_frmad.c) for an example of how to create a frame-based S-function. This S-function is one of several S-functions that manipulate frame-based signals found in the Simulink model sfcndemo_frame.mdl.
In a Level-2 M-file S-function, set the SamplingMode property of the port to indicate if the block accepts frame-based signals, for example:
block.InputPort(1).SamplingMode = 'Inherited';
If any of the ports inherited their sampling mode, define a SetInputPortSamplingMode callback method to specify the sampling mode.
![]() | Sim Viewing Devices in External Mode | Error Handling | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |