Main Content

S功能示例

Overview of Examples

To run an example:

  1. In the MATLAB®Command Window, entersfundemos

    这S-function example library opens.

    Each block represents a category of S-function examples.

  2. Double-click a category to display the examples that it includes. For example, clickC-files

  3. Double-click a block to open and run the example that it represents.

It might be helpful to examine some sample S-functions as you read the next chapters. Code for the examples is stored in the following folder under the MATLAB root folder.

MATLAB code

toolbox/simulink/simdemos/simfeatures

C, C++, and Fortran code

toolbox/simulink/simdemos/simfeatures/src

2级MATLAB S功能示例

matlabroot/toolbox/simulink/simdemos/simfeatures文件夹 (open)包含许多2级MATLAB S功能。考虑从查看这些文件开始。

Filename 型号名称 Description
MSFCN_DSC.M MSFCNDEMO_SFUNDSC1 Implement an S-function with an inherited sample time.
MSFCN_LIMINTM.M msfcndemo_limintm Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
msfcn_multirate.m MSFCNDEMO_MULTIRATE Implement a multirate system.
msfcn_times_two.m msfcndemo_timestwo Implement an S-function that doubles its input.
msfcn_unit_delay.m MSFCNDEMO_SFUNDSC2 Implement a unit delay.
MSFCN_VARPULSE.M MSFCNDEMO_VARPULSE Implement a variable pulse width generator by callingset_param从2级MATLAB S功能中。还演示了如何使用自定义集并获取块SimState的方法。
msfcn_vs.m msfcndemo_vsfunc Implement a variable sample time block in which the first input is delayed by an amount of time determined by the second input.

1级MATLAB S功能示例

matlabroot/toolbox/simulink/simdemos/simfeatures文件夹 (open)还包含许多Level-1 Matlab S功能,作为旧模型的参考。这些Level-1 MATLAB S函数中的大多数没有相关的示例模型。

Filename Description
csfunc.m Define a continuous system in state-space format.
dsfunc.m Define a discrete system in state-space format.
limintm.m Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions.
mixedm.m 实现由单位延迟串联的连续集成器组成的混合系统。
sfun_varargm.m Implement an S-function that shows how to use the MATLAB commandvarargin
vsfunc.m Illustrate how to create a variable sample time block. This S-function implements a variable step delay in which the first input is delayed by an amount of time determined by the second input.

C函数示例

matlabroot/toolbox/simulink/simdemos/simfeatures/src文件夹 (open)包含C MEX S函数的示例,其中许多具有MATLAB S功能对应物。下表中列出了C MEX S函数。

Filename 型号名称 Description
csfunc.c sfcndemo_csfunc Implement a continuous system.
dlimintc.c No model available 实施离散时间有限的集成器。
dsfunc.c sfcndemo_dsfunc 实施离散系统。
limintc.c No model available 实施有限的集成商。
mixedm.c sfcndemo_mixedm Implement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z).
Mixhmex.C sfcndemo_mixedmex 实现具有单个输出和两个输入的混合动态系统。
slexQuantizeSFcn.c sfcndemo_sfun_quantize 实施矢量化量化器。按照量化间隔参数指定的步骤将输入量化为步骤
sdotproduct.C sfcndemo_sdotproduct 两个真实或复杂向量的计算点产物(多重蓄电)。
sfbuilder_bususage.c sfbuilder_bususage Access S-Function Builder with a bus input and output.
sfbuilder_movingAverage.c sfbuilder_movingAverage Implement simple time window moving average using开始andTerminate
sftable2.c sfcndemo_sftable2 Implement a two-dimensional table lookup.
sfun_atol.c sfcndemo_sfun_atol 为每个继续设置不同的绝对误差ous state.
sfun_cplx.c sfcndemo_cplx Add complex data for an S-function with one input port and one parameter.
sfun_directlook.c No model available 实施直接的1D查找。
sfun_dtype_io.c sfcndemo_dtype_io Implement an S-function that uses Simulink®输入和输出的数据类型。
sfun_dtype_param.c sfcndemo_dtype_param 实施一个使用simulink数据类型进行参数的s功能。金宝app
sfun_dynsize.c sfcndemo_sfun_dynsize Implements dynamically-sized outputs .
sfun_errhdl.c sfcndemo_sfun_errhdl Check parameters using themdlCheckParametersS-function routine.
sfun_fcncall.c sfcndemo_sfun_fcncall Execute function-call subsystems on the first and second output elements.
sfun_frmad.c sfcndemo_frame Implement a frame-based A/D converter.
sfun_frmda.c sfcndemo_frame Implement a frame-based D/A converter.
sfun_frmdft.c sfcndemo_frame 实施基于多通道框架的离散转换(及其倒数)。
sfun_frmunbuff.c sfcndemo_frame Implement a frame-based unbuffer block.
sfun_multiport.c sfcndemo_sfun_multiport Configure multiple input and output ports.
sfun_manswitch.c No model available 实现手动开关。
sfun_matadd.c sfcndemo_matadd 在一个输入端口,一个输出端口和一个参数中添加矩阵。
sfun_multirate.c SFCNDEMO_SFUN_MULTIRATE Demonstrate how to specify port-based sample times.
sfun_port_constant.c sfcndemo_port_constant 演示如何指定基于恒定的端口样本时间。
sfun_port_triggered.c SFCNDEMO_PORT_TRIGGED Demonstrate how to use port-based sample times in a triggered subsystem.
sfun_runtime1.c sfcndemo_runtime Implement run-time parameters for all tunable parameters.
sfun_runtime2.c sfcndemo_runtime 注册单个运行时参数。
sfun_runtime3.c sfcndemo_runtime Register dialog parameters as run-time parameters.
sfun_runtime4.c sfcndemo_runtime Implement run-time parameters as a function of multiple dialog parameters.
sfun_simstate.c No model available 演示用于保存和恢复SimState的S功能API。
sfun_zc.c sfcndemo_sfun_zc 证明使用非采样零交叉来实施abs(u)。This S-function is designed to be used with a variable-step solver.
sfun_zc_sat.c sfcndemo_sfun_zc_sat Demonstrate zero crossings with saturation.
sfun_zc_cstate_sat.c sfcndemo_sfun_zc_cstate_sat Implement a continuous integrator with saturation limits and zero-crossing detection.
sfun_integrator_localsolver.c sfcndemo_sfun_localsolver 演示连续的积分器,其中使用单独的局部求解器而不是模型使用的求解器求解连续状态。
sfun_angle_events.c sfcndemo_angle_events 实施一种方法,以稳健有效地检测旋转的身体交叉指定角度。
sfun_angle_events.c No model available Demonstrate angle detection and incorporate Stateflow®安排功能调用。
sfunmem.c sfcndemo_sfunmem 实现一个one-integration-step延迟和抱着我mory function.
simomex.c sfcndemo_simomex

Implement a single-input, two-output state-space dynamic system described by the state-space equations:

dx/dt = ax + bu y = cx + du
whereX是国家向量,is vector of inputs, andy是输出的向量。

stspace.c sfcndemo_stspace Implement a set of state-space equations. You can turn this into a new block by using the S-Function block and mask facility. This example MEX file performs the same function as the built-in State-Space block. This is an example of a MEX file where the number of inputs, outputs, and states is dependent on the parameters passed in from the workspace.
stvctf.c sfcndemo_stvctf Implement a continuous-time transfer function whose transfer function polynomials are passed in via the input vector. This is useful for continuous time adaptive control applications.
stvdtf.c sfcndemo_stvdtf Implement a discrete-time transfer function whose transfer function polynomials are passed in via the input vector. This is useful for discrete-time adaptive control applications.
stvmgain.c sfcndemo_stvmgain Implement a time-varying matrix gain.
table3.c No model available Implement a 3-D lookup table.
timestwo.c sfcndemo_timestwo Implement a C MEX S-function that doubles its input.
vdlmintc.c No model available Implement a discrete-time vectorized limited integrator.
vdpmex.c sfcndemo_vdpmex 实现范德尔方程。
vlimintc.c No model available 实施矢量有限积分器。
vsfunc.c sfcndemo_vsfunc Illustrate how to create a variable sample time block. This block implements a variable-step delay in which the first input is delayed by an amount of time determined by the second input.
sfun_pwm.c sfcndemo_pwm 说明如何创建可控的样本时间块。
sfun_d2c sfcndemo_d2c Illustrate how to convert a discrete input signal into a smooth continuous output signal

fortran s功能示例

这following table lists sample Fortran S-functions available in thematlabroot/toolbox/simulink/simdemos/simfeatures/src文件夹 (open)。

Filename 型号名称

Description

sfun_atmos.c
sfun_atmos_sub.f
sfcndemo_atmos Calculate the 1976 standard atmosphere to 86 km using a Fortran subroutine.

C++ S-Function Examples

下表列出了在matlabroot/toolbox/simulink/simdemos/simfeatures/src文件夹 (open)。

Filename 型号名称 Description
sfun_counter_cpp.cpp sfcndemo_counter_cpp 将C ++对象存储在Pointers矢量中PWork
sfbuilder_permute.cpp sfbuilder_permunt Implement permutation by calling external C++ classes using Start and Terminate.
sfbuilder_linfilt.cpp sfbuilder_upsmpling Implement linear filtering with C++ STL algorithms for the upsampling of a sign wave.

Organizing S-Function in a Project

下面的示例显示了如何将S功能伪像安排到项目中。该项目可以作为邮政编码或MATLAB工具箱与他人共享。

Filename Description
slexBlocksetDesignerExample Organize S-function artifacts into a single project using Blockset Designer.

也可以看看

|||

Related Topics