文档

Input and Output Ports

为C S函数创建输入端口

创建和配置input ports, themdlInitializeSizes方法应首先使用S函数输入端口的数量ssSetNumInputPorts。这n,,,,for each input port, the method should specify

  • 这dimensions of the input port (seeInitializing Input Port Dimensions

    如果您希望您的s功能从其连接到的端口继承其维度,则应指定端口的动态尺寸mdlInitializeSizes(seeSizing an Input Port Dynamically)。

  • Whether the input port allows scalar expansion of inputs (seeScalar Expansion of Inputs

  • Whether the input port has direct feedthrough, usingssSetInputPortDirectFeedThrough

    A port has direct feedthrough if the input is used in either themdlOutputsormdlGetTimeOfNextVarHit功能。每个输入端口的直接进料标志都可以设置为1=yesor0 =否。It should be set to 1 if the input,,,,,is used in themdlOutputsormdlGetTimeOfNextVarHit常规。将直接进料标志设置为0告诉Simulink金宝app®engine that在这两个S功能例程中均未使用。违反这会导致不可预测的结果。

  • 输入端口的数据类型,如果不是默认double

    UsesssetInputportDataType设置输入端口的数据类型。如果您希望端口的数据类型取决于其连接到的端口的数据类型,请将数据类型指定为DYNAMICALLY_TYPED。In this case, you must provide implementations of themdlSetInputPortDataTypeandMDLSETDEFAULTPORTDATATATYPESmethods to enable the data type to be set correctly during signal propagation.

  • 如果端口接受复杂值信号,则输入端口的数字类型

    UsesssetInputportPortComplexSignalto set the input port's numeric type. If you want the numeric type of the port to depend on the numeric type of the port to which it is connected, specify the numeric type ascomplex_inherited。In this case, you must provide implementations of themdlSetInputPortComplexSignalandmdlSetDefaultPortComplexSignalsmethods to enable the numeric type to be set correctly during signal propagation.

You can configure additional input port properties using other S-function macros. SeeInput and Output Portsin the “SimStruct Macros and Functions Listed by Usage” section for more information.

笔记

mdlInitializeSizesmethod must specify the number of ports before setting any properties. If it attempts to set a property of a port that doesn't exist, it is accessing invalid memory and a segmentation violation occurs.

Initializing Input Port Dimensions

您可以使用以下宏之一设置输入端口尺寸:

  • 如果输入信号必须是一维,并且输入端口宽度为w,,,,你se

    sssetInputPortWidth(S,InputPortIDX,W)
  • If the input signal must be a matrix of dimensionm-by-n,,,,你se

    sssetInputportMatrixDimensions(S, inputPortIdx, m, n)
  • 否则,如果输入信号具有一个或两个维度,请使用

    sssetInputportDimensionInfo(S,InputPortIdx,Dimsinfo)

    You can use this function to fully or partially initialize the port dimensions (see next section).

Sizing an Input Port Dynamically

如果你的s函数does not require that its input signals have specific dimensions, you can set the dimensionality of the input ports to match the dimensionality of the signals connected to them.

To dynamically dimension an input port:

  • 指定输入端口的某些或全部尺寸为动态尺寸mdlInitializeSizes

    If the input port can accept a signal of any dimensionality, use

    sssetInputportDimensionInfo(S, inputPortIdx, DYNAMIC_DIMENSION)

    to set the dimensionality of the input port.

    If the input port can accept only vector (1-D) signals but the signals can be of any size, use

    sssetInputPortWidth(S, inputPortIdx, DYNAMICALLY_SIZED)

    to specify the dimensionality of the input port.

    如果输入端口只能接受矩阵信号,但可以接受任何行或列大小,请使用

    sssetInputportMatrixDimensions(S, inputPortIdx, DYNAMICALLY_SIZED, DYNAMICALLY_SIZED)
  • Provide anmdlSetInputPortDimensionInfo将输入端口的尺寸设置为连接到它的信号的大小的方法。

    这Simulink engine invokes this method during signal propagation when it has determined the dimensionality of the signal connected to the input port.

  • Provide anmdlsetDefaultPortDimensionInfomethod that sets the dimensions of the block's ports to a default value. Seesfun_dynsize.cfor an example that implements this macro.

    这engine invokes this method during signal propagation when it cannot determine the dimensionality of the signal connected to some or all of the block's input ports. This can happen, for example, if an input port is unconnected. If the S-function does not provide this method, the signal propagation routine sets the dimension of the block's ports to 1-D scalar.

Example: Defining Multiple S-Function Input Ports

这following code inmdlInitializeSizesconfigures an S-function with two input ports. SeeInput and Output Portsin the “SimStruct Macros and Functions Listed by Usage” section for more information on the macros used in this example.

如果(!sssetNuminputports(s,2))返回;for(i = 0; i <2; i ++){ / *输入具有直接的feedThrough * / sssetInputPortDirectFeedThrough(s,i,1);/ *输入是一个真实信号 */ sssetInputportComplexSignal(s,i,conseft_no);/ *输入是一个动态尺寸的2-D矩阵 */ sssetInputportMatrixDimensions(s,i,dynamiedally_sized,dynamilly_sized);/ *输入继承其示例时间 */ sssetInputportSampletime(s,i,carenited_sample_time);/ *输入信号必须是连续的 */ sssetInputportrequiredContiguul(s,i,1);/ *输入端口无法共享内存 */ sssetInputPortoverrable(s,i,0);}

在信号传播期间,Simulink引擎称此S功能为金宝appmdlSetInputPortDimensionInfo宏以初始化输入端口尺寸。在此示例中mdlSetInputPortDimensionInfosets the input dimensions to the candidate dimensions passed to the macro by the engine.

#if定义(matlab_mex_file)#define mdl_set_input_port_dimension_info static static void mdlsetInputportdimensionInfo(simStruct *s,int_t port,const dimsinfo_t *dimsinfo)} #万一

For an example that configures an S-function with multiple input and output ports, open the Simulink modelsfcndemo_sfun_multiportand inspect the S-functionsfun_multiport.c

为C S函数创建输出端口

创建和配置output ports, themdlInitializeSizesmethod should first specify the number of S-function output ports, usingssSetNumOutputPorts。这n,,,,for each output port, the method should specify

为C S函数创建输入端口有关如何初始化S函数输入端口的示例。您使用相同的过程来初始化S功能输出端口,但使用相应的输出端口宏。

Scalar Expansion of Inputs

输入的标量扩展在概念上是指将标量输入信号扩展到与连接到其他S函数输入端口的宽输入信号相同的维度的过程。这是通过将扩展信号的每个元素设置为标量输入的值来完成的。

A Level-2 MATLAB®S-function uses the default scalar expansion rules if the input and output ports are specified as dynamically sized (seeScalar Expansion of Inputs and ParametersinUsing Simulink)。

With scalar expansion on, the S-functionmdlInitializeSizesmethod should specify that the input and output ports are dynamically sized. The Simulink engine uses a default method to set the dimensions of the input and output ports. If the block has more than two inputs, the input signals can be scalar or wide signals, where the wide signals all have the same number of elements. In this case, the engine sets the dimensions of the output ports to the width of the wide input signals and expands any scalar inputs to this width. If the wide inputs are driven by 1-D and 2-D vectors, the output is a 2-D vector signal, and the scalar inputs are expanded to a 2-D vector signal.

If scalar expansion is not on, the engine assumes that all ports (input and output ports) must have the same dimensions, and it sets all port dimensions to the same dimensions specified by one of the driving blocks.

笔记

这engine ignores the scalar expansion option if the S-function specifies or controls the dimensions of its input and output ports either by initializing the dimensions inmdlInitializeSizes,,,,你singmdlsetInputPortWidthandmdlSetOutputPortWidth,,,,or usingmdlSetInputPortDimensionInfo,,,,mdlSetOutputPortDimensionInfo,,,,andmdlsetDefaultPortDimensionInfo

这best way to understand how to use scalar expansion is to consider the examplesfcndemo_sfun_multiport。This model contains three S-function blocks, each with multiple input ports. The S-functionsfun_multiport.c用于这些块设置ss_option_allow_input_scalar_expansionoption in itsmdlInitializeSizesmethod, allowing scalar expansion of the inputs. The S-function specifies that its inputs and outputs are dynamically sized. Therefore, during signal propagation, the engine sets the width of the input ports to the width of the signal connected to the port, and the width of the output ports to the width of any wide input signal. ThemdlOutputs方法在输入信号上执行逐元元素总和,根据需要扩展任何标量输入。

/* Calculate an element-by-element sum of the input signals. yWidth is the width of the output signal. */ for (el = 0; el < yWidth; el++) { int_T port; real_T sum = 0.0; for (port = 0; port < nInputPorts; port++) { /* Get the input signal value */ InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,port); if (el < ssGetInputPortWidth(S,port)) { /* Input is a wide signal. Use specific element */ sum = sum + ((real_T)signs[port] * (*uPtrs[el])); } else { /* Use the scalar value to expand the signal */ sum = sum + ((real_T)signs[port] * (*uPtrs[0])); } } }

Masked Multiport S-Functions

如果您正在开发蒙版的多端子S功能块,其端口数量会根据某些参数而变化,并希望将它们放在Simulink库中,则必须指定掩码修改块的外观。金宝app为此,执行命令

set_param(blockName,,,,'MaskSelfModifiable','on')

at the MATLAB command prompt before saving the library, whereblockNameis the full path to the block. Failure to specify that the mask modifies the appearance of the block means that an instance of the block in a model reverts to the number of ports in the library whenever you load the model or update the library link.