mdlInitializeSizes
Specify the number of inputs, outputs, states, parameters, and other characteristics of the C MEX S-function
Required
Yes
Languages
C, C++
Syntax
#define MDL_INITIAL_SIZES
void mdlInitializeSizes(SimStruct *S)
Arguments
S
SimStruct representing an S-Function block.
Description
This is the first S-function callback methods that the Simulink® engine calls. This method performs the following tasks:
Specify the number of parameters that this S-function supports, using
ssSetNumSFcnParams
.Use
ssSetSFcnParamTunable(S,paramIdx, 0)
when a parameter cannot change during simulation, whereparamIdx
starts at0
. When a parameter has been specified as not tunable, the engine issues an error during simulation (or when in external mode when using the Simulink Coder™ product) if an attempt is made to change the parameter.Specify the number of states that this function has, using
ssSetNumContStates
andssSetNumDiscStates
.Configure the block's input ports, including:
Specify the number of input ports that this S-function has, using
ssSetNumInputPorts
.Specify the dimensions of the input ports.
See
ssSetInputPortDimensionInfo
for more information.For each input port, specify whether it has direct feedthrough, using
ssSetInputPortDirectFeedThrough
.A port has direct feedthrough if the input is used in either the
mdlOutputs
ormdlGetTimeOfNextVarHit
function. The direct feedthrough flag for each input port can be set to either1=yes
or0=no
. It should be set to 1 if the input,u
, is used in themdlOutputs
ormdlGetTimeOfNextVarHit
routine. Setting the direct feedthrough flag to 0 tells the Simulink engine thatu
is not used in either of these S-function routines. Violating this leads to unpredictable results.
Configure the block's output ports, including:
Specify the number of output ports that the block has, using
ssSetNumOutputPorts
.Specify the dimensions of the output ports.
See
mdlSetOutputPortDimensionInfo
for more information.
If your S-function outputs are discrete (for example, the outputs only take specific values such as 0, 1, and 2), specify
SS_OPTION_DISCRETE_VALUED_OUTPUT
.Set the number of sample times (i.e., sample rates) at which the block operates.
There are two ways of specifying sample times:
Port-based sample times
Block-based sample times
See Specify S-Function Sample Times for a complete discussion of sample time issues.
For multirate S-functions, the suggested approach to setting sample times is via the port-based sample times method. When you create a multirate S-function, you must take care to verify that, when slower tasks are preempted, your S-function correctly manages data so as to avoid race conditions. When port-based sample times are specified, the block cannot inherit a sample time of
Inf
at any port.Set the size of the block's work vectors, using
ssSetNumRWork
,ssSetNumIWork
,ssSetNumPWork
,ssSetNumModes
,ssSetNumNonsampledZCs
.Set the simulation options that this block implements, using
ssSetOptions
.All options have the form
SS_OPTION_<name>
. See Configure C/C++ S-Function Features for information on each option. Use a bitwiseOR
operator to set multiple options, as inssSetOptions(S, (SS_OPTION_name1 | SS_OPTION_name2))
Note
If you have Simulink
Coder, when generating code for a noninlined S-function that contains this method,
make sure the method is not wrapped in a #if defined(MATLAB_MEX_FILE)
statement. For example:
#if defined(MATLAB_MEX_FILE) static void mdlInitializeSizes(SimStruct *S) { /* Add mdlInitializeSizes code here * } #endif
The define
statement makes the
mdlInitializeSizes
method available only to a MATLAB® MEX file. If the S-function is not inlined, the Simulink
Coder product cannot use this method, resulting in link or run-time errors.
Dynamically Sized Block Features
You can set the parameters NumContStates
,
NumDiscStates
, NumInputs
,
NumOutputs
, NumRWork
, NumIWork
,
NumPWork
, NumModes
, and
NumNonsampledZCs
to a fixed nonnegative integer or tell the
Simulink engine to size them dynamically:
DYNAMICALLY_SIZED
-- Sets lengths of states, work vectors, and so on to values inherited from the driving block. It sets widths to the actual input widths, according to the scalar expansion rules unless you usemdlSetWorkWidths
to set the widths.0
or positive number -- Sets lengths (or widths) to the specified values. The default is0
.
Initialization for MATLAB S-Functions
The Level-2 MATLAB S-function setup
method performs nearly the same tasks as the C MEX S-function
mdlInitializeSizes
method.
Examples
static void mdlInitializeSizes(SimStruct *S) { int_T nInputPorts = 1; /* number of input ports */ int_T nOutputPorts = 1; /* number of output ports */ int_T needsInput = 1; /* direct feedthrough */ int_T inputPortIdx = 0; int_T outputPortIdx = 0; ssSetNumSFcnParams(S, 0); /* Number of expected parameters */ if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { /* * If the number of expected input parameters is not * equal to the number of parameters entered in the * dialog box, return. The Simulink engine generates an * error indicating that there is aparameter mismatch. */ return; }else { mdlCheckParameters(S); if (ssGetErrorStatus(S) != NULL) return; } ssSetNumContStates( S, 0); ssSetNumDiscStates( S, 0); /* * Configure the input ports. First set the number of input * ports. */ if (!ssSetNumInputPorts(S, nInputPorts)) return; /* * Set input port dimensions for each input port index * starting at 0. */ if(!ssSetInputPortDimensionInfo(S, inputPortIdx, DYNAMIC_DIMENSION)) return; /* * Set direct feedthrough flag (1=yes, 0=no). */ ssSetInputPortDirectFeedThrough(S, inputPortIdx, needsInput); /* * Configure the output ports. First set the number of * output ports. */ if (!ssSetNumOutputPorts(S, nOutputPorts)) return; /* * Set output port dimensions for each output port index * starting at 0. */ if(!ssSetOutputPortDimensionInfo(S,outputPortIdx, DYNAMIC_DIMENSION)) return; /* * Set the number of sample times. */ ssSetNumSampleTimes(S, 1); /* * Set size of the work vectors. */ ssSetNumRWork(S, 0); /* real vector */ ssSetNumIWork(S, 0); /* integer vector */ ssSetNumPWork(S, 0); /* pointer vector */ ssSetNumModes(S, 0); /* mode vector */ ssSetNumNonsampledZCs(S, 0); /* zero crossings */ ssSetOptions(S, 0); } /* end mdlInitializeSizes */
See Also
Version History
Introduced before R2006a