To run an example:
In the MATLAB^{®} Command Window, enter sfundemossfundemos
.
The Sfunction example library opens.
Each block represents a category of Sfunction examples.
Doubleclick a category to display the examples that it includes. For example, click Cfiles.
Doubleclick a block to open and run the example that it represents.
It might be helpful to examine some sample Sfunctions as you read the next chapters. Code for the examples is stored in the following folder under the MATLAB root folder.
MATLAB code 

C, C++, and Fortran code 

The
folder contains many
Level2 MATLAB Sfunctions.
Consider starting off by looking at these files.matlabroot
/toolbox/simulink/simdemos/simfeatures
Filename  Model Name  Description 

msfcn_dsc.mmsfcn_dsc.m  msfcndemo_sfundsc1msfcndemo_sfundsc1  Implement an Sfunction with an inherited sample time. 
msfcn_limintm.mmsfcn_limintm.m  msfcndemo_limintmmsfcndemo_limintm  Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. 
msfcn_multirate.m  msfcndemo_multiratemsfcndemo_multirate  Implement a multirate system. 
msfcn_times_two.mmsfcn_times_two.m  msfcndemo_timestwomsfcndemo_timestwo  Implement an Sfunction that doubles its input. 
msfcn_unit_delay.mmsfcn_unit_delay.m  msfcndemo_sfundsc2msfcndemo_sfundsc2  Implement a unit delay. 
msfcn_varpulse.mmsfcn_varpulse.m  msfcndemo_varpulsemsfcndemo_varpulse  Implement a variable pulse width generator by calling set_param from
within a Level2 MATLAB Sfunction.
Also demonstrates how to use custom set and get methods for the block
SimState. 
msfcn_vs.mmsfcn_vs.m  msfcndemo_vsfuncmsfcndemo_vsfunc  Implement a variable sample time block in which the first input is delayed by an amount of time determined by the second input. 
The
folder also contains
many Level1 MATLAB Sfunctions,
provided as reference for legacy models. Most of these Level1 MATLAB Sfunctions do not have associated
example models.matlabroot
/toolbox/simulink/simdemos/simfeatures
Filename  Description 

csfunc.mcsfunc.m  Define a continuous system in statespace format. 
dsfunc.mdsfunc.m  Define a discrete system in statespace format. 
limintm.mlimintm.m  Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. 
mixedm.mmixedm.m  Implement a hybrid system consisting of a continuous integrator in series with a unit delay. 
sfun_varargm.msfun_varargm.m  Implement an Sfunction that shows how to use the MATLAB command varargin . 
vsfunc.mvsfunc.m  Illustrate how to create a variable sample time block. This Sfunction implements a variable step delay in which the first input is delayed by an amount of time determined by the second input. 
The
folder contains examples
of C MEX Sfunctions, many of which have a MATLAB Sfunction
counterpart. The C MEX Sfunctions are listed in the following table.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename  Model Name  Description 

csfunc.ccsfunc.c  sfcndemo_csfuncsfcndemo_csfunc  Implement a continuous system. 
dlimintc.cdlimintc.c  No model available  Implement a discretetime limited integrator. 
dsfunc.cdsfunc.c  sfcndemo_dsfuncsfcndemo_dsfunc  Implement a discrete system. 
limintc.climintc.c  No model available  Implement a limited integrator. 
mixedm.cmixedm.c  sfcndemo_mixedmsfcndemo_mixedm  Implement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z). 
mixedmex.cmixedmex.c  sfcndemo_mixedmexsfcndemo_mixedmex  Implement a hybrid dynamic system with a single output and two inputs. 
slexQuantizeSFcn.cslexQuantizeSFcn.c  sfcndemo_sfun_quantizesfcndemo_sfun_quantize  Implement a vectorized quantizer. Quantizes the input into
steps as specified by the quantization interval parameter, q . 
sdotproduct.csdotproduct.c  sfcndemo_sdotproductsfcndemo_sdotproduct  Compute dot product (multiplyaccumulate) of two real or complex vectors. 
sfbuilder_bususage.csfbuilder_bususage.c  sfbuilder_bususagesfbuilder_bususage  Access SFunction Builder with a bus input and output. 
sftable2.csftable2.c  No model available  Implement a twodimensional table lookup. 
sfun_atol.csfun_atol.c  sfcndemo_sfun_atolsfcndemo_sfun_atol  Set different absolute tolerances for each continuous state. 
sfun_cplx.csfun_cplx.c  sfcndemo_cplxsfcndemo_cplx  Add complex data for an Sfunction with one input port and one parameter. 
sfun_directlook.csfun_directlook.c  No model available  Implement a direct 1D lookup. 
sfun_dtype_io.csfun_dtype_io.c  sfcndemo_dtype_iosfcndemo_dtype_io  Implement an Sfunction that uses Simulink^{®} data types for inputs and outputs. 
sfun_dtype_param.csfun_dtype_param.c  sfcndemo_dtype_paramsfcndemo_dtype_param  Implement an Sfunction that uses Simulink data types for parameters. 
sfun_dynsize.csfun_dynsize.c  sfcndemo_sfun_dynsizesfcndemo_sfun_dynsize  Implements dynamicallysized outputs . 
sfun_errhdl.csfun_errhdl.c  sfcndemo_sfun_errhdlsfcndemo_sfun_errhdl  Check parameters using the mdlCheckParameters Sfunction
routine. 
sfun_fcncall.csfun_fcncall.c  sfcndemo_sfun_fcncallsfcndemo_sfun_fcncall  Execute functioncall subsystems on the first and second output elements. 
sfun_frmad.csfun_frmad.c  sfcndemo_framesfcndemo_frame  Implement a framebased A/D converter. 
sfun_frmda.csfun_frmda.c  sfcndemo_framesfcndemo_frame  Implement a framebased D/A converter. 
sfun_frmdft.csfun_frmdft.c  sfcndemo_framesfcndemo_frame  Implement a multichannel framebased DiscreteFourier transformation (and its inverse). 
sfun_frmunbuff.csfun_frmunbuff.c  sfcndemo_framesfcndemo_frame  Implement a framebased unbuffer block. 
sfun_multiport.csfun_multiport.c  sfcndemo_sfun_multiportsfcndemo_sfun_multiport  Configure multiple input and output ports. 
sfun_manswitch.csfun_manswitch.c  No model available  Implement a manual switch. 
sfun_matadd.csfun_matadd.c  sfcndemo_mataddsfcndemo_matadd  Add matrices in an Sfunction with one input port, one output port, and one parameter. 
sfun_multirate.csfun_multirate.c  sfcndemo_sfun_multiratesfcndemo_sfun_multirate  Demonstrate how to specify portbased sample times. 
sfun_port_constant.csfun_port_constant.c  sfcndemo_port_constantsfcndemo_port_constant  Demonstrate how to specify constant portbased sample times. 
sfun_port_triggered.csfun_port_triggered.c  sfcndemo_port_triggeredsfcndemo_port_triggered  Demonstrate how to use portbased sample times in a triggered subsystem. 
sfun_runtime1.csfun_runtime1.c  sfcndemo_runtimesfcndemo_runtime  Implement runtime parameters for all tunable parameters. 
sfun_runtime2.csfun_runtime2.c  sfcndemo_runtimesfcndemo_runtime  Register individual runtime parameters. 
sfun_runtime3.csfun_runtime3.c  sfcndemo_runtimesfcndemo_runtime  Register dialog parameters as runtime parameters. 
sfun_runtime4.csfun_runtime4.c  sfcndemo_runtimesfcndemo_runtime  Implement runtime parameters as a function of multiple dialog parameters. 
sfun_simstate.csfun_simstate.c  sfcndemo_sfun_simstatesfcndemo_sfun_simstate  Demonstrate the Sfunction API for saving and restoring the SimState. 
sfun_zc.csfun_zc.c  sfcndemo_sfun_zcsfcndemo_sfun_zc  Demonstrate use of nonsampled zero crossings to implement abs(u) .
This Sfunction is designed to be used with a variablestep solver. 
sfun_zc_sat.csfun_zc_sat.c  sfcndemo_sfun_zc_satsfcndemo_sfun_zc_sat  Demonstrate zero crossings with saturation. 
sfunmem.csfunmem.c  sfcndemo_sfunmemsfcndemo_sfunmem  Implement a oneintegrationstep delay and hold memory function. 
simomex.csimomex.c  sfcndemo_simomexsfcndemo_simomex  Implement a singleinput, twooutput statespace dynamic system
described by the statespace equations:dx/dt = Ax + Bu y = Cx + Du x is the state vector, u is
vector of inputs, and y is the vector of outputs. 
stspace.cstspace.c  sfcndemo_stspacesfcndemo_stspace  Implement a set of statespace equations. You can turn this into a new block by using the SFunction block and mask facility. This example MEX file performs the same function as the builtin StateSpace 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.cstvctf.c  sfcndemo_stvctfsfcndemo_stvctf  Implement a continuoustime transfer function whose transfer function polynomials are passed in via the input vector. This is useful for continuous time adaptive control applications. 
stvdtf.cstvdtf.c  sfcndemo_stvdtfsfcndemo_stvdtf  Implement a discretetime transfer function whose transfer function polynomials are passed in via the input vector. This is useful for discretetime adaptive control applications. 
stvmgain.cstvmgain.c  sfcndemo_stvmgainsfcndemo_stvmgain  Implement a timevarying matrix gain. 
table3.ctable3.c  No model available  Implement a 3D lookup table. 
timestwo.ctimestwo.c  sfcndemo_timestwosfcndemo_timestwo  Implement a C MEX Sfunction that doubles its input. 
vdlmintc.cvdlmintc.c  No model available  Implement a discretetime vectorized limited integrator. 
vdpmex.cvdpmex.c  sfcndemo_vdpmexsfcndemo_vdpmex  Implement the Van der Pol equation. 
vlimintc.cvlimintc.c  No model available  Implement a vectorized limited integrator. 
vsfunc.cvsfunc.c  sfcndemo_vsfuncsfcndemo_vsfunc  Illustrate how to create a variable sample time block. This block implements a variablestep delay in which the first input is delayed by an amount of time determined by the second input. 
The following table lists sample Fortran Sfunctions available
in the
folder.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename  Model Name  Description 

sfun_timestwo_for.Fsfun_timestwo_for.F  sfcndemo_timestwo_forsfcndemo_timestwo_for  Implement a Level1 Fortran Sfunction that represents the timestwo.c Sfunction. 
sfun_atmos.csfun_atmos.c sfun_atmos_sub.Fsfun_atmos_sub.F  sfcndemo_atmossfcndemo_atmos  Calculate the 1976 standard atmosphere to 86 km using a Fortran subroutine. 
The following table lists sample C++ Sfunctions available in
the
folder.matlabroot
/toolbox/simulink/simdemos/simfeatures/src
Filename  Model Name  Description 

sfun_counter_cpp.cppsfun_counter_cpp.cpp  sfcndemo_counter_cppsfcndemo_counter_cpp  Store a C++ object in the pointers vector PWork . 