| Contents | Index |
| On this page… |
|---|
A template file for Fortran MEX S-functions is located at sfuntmpl_fortran.F. The template file compiles as is and copies the input to the output.
To use the template to create a new Fortran S-function:
Compile the edited file into a MEX-file, using the mex command.
Include the MEX-file in your model, using the S-Function block.
The example file, sfun_timestwo_for.F, implements an S-function that multiplies its input by 2.
C
C File: SFUN_TIMESTWO_FOR.F
C
C Abstract:
C A sample Level-1 FORTRAN representation of a
C timestwo S-function.
C
C The basic mex command for this example is:
C
C >> mex sfun_timestwo_for.F simulink.F
C
C Copyright 1990-2002 The MathWorks, Inc.
C
C
C
C=====================================================
C Function: SIZES
C
C Abstract:
C Set the size vector.
C
C SIZES returns a vector which determines model
C characteristics. This vector contains the
C sizes of the state vector and other
C parameters. More precisely,
C SIZE(1) number of continuous states
C SIZE(2) number of discrete states
C SIZE(3) number of outputs
C SIZE(4) number of inputs
C SIZE(5) number of discontinuous roots in
C the system
C SIZE(6) set to 1 if the system has direct
C feedthrough of its inputs,
C otherwise 0
C
C=====================================================
C
SUBROUTINE SIZES(SIZE)
C .. Array arguments ..
INTEGER*4 SIZE(*)
C .. Parameters ..
INTEGER*4 NSIZES
PARAMETER (NSIZES=6)
SIZE(1) = 0
SIZE(2) = 0
SIZE(3) = 1
SIZE(4) = 1
SIZE(5) = 0
SIZE(6) = 1
RETURN
END
C
C=====================================================
C
C Function: OUTPUT
C
C Abstract:
C Perform output calculations for continuous
C signals.
C
C=====================================================
C .. Parameters ..
SUBROUTINE OUTPUT(T, X, U, Y)
REAL*8 T
REAL*8 X(*), U(*), Y(*)
Y(1) = U(1) * 2.0
RETURN
END
C
C=====================================================
C
C Stubs for unused functions.
C
C=====================================================
SUBROUTINE INITCOND(X0)
REAL*8 X0(*)
C --- Nothing to do.
RETURN
END
SUBROUTINE DERIVS(T, X, U, DX)
REAL*8 T, X(*), U(*), DX(*)
C --- Nothing to do.
RETURN
END
SUBROUTINE DSTATES(T, X, U, XNEW)
REAL*8 T, X(*), U(*), XNEW(*)
C --- Nothing to do.
RETURN
END
SUBROUTINE DOUTPUT(T, X, U, Y)
REAL*8 T, X(*), U(*), Y(*)
C --- Nothing to do.
RETURN
END
SUBROUTINE TSAMPL(T, X, U, TS, OFFSET)
REAL*8 T,TS,OFFSET,X(*),U(*)
C --- Nothing to do.
RETURN
END
SUBROUTINE SINGUL(T, X, U, SING)
REAL*8 T, X(*), U(*), SING(*)
C --- Nothing to do.
RETURN
END
A Level-1 S-function's input/output is limited to using the REAL*8 data type, (DOUBLE PRECISION), which is equivalent to a double in C. Of course, the internal calculations can use whatever data types you need.
To see how this S-function works, enter
sfcndemo_timestwo_for
at the MATLAB command prompt and run the model.
Simulink Coder users can use the sample block target file sfun_timestwo_for.tlc to generate inlined code for sfcndemo_timestwo_for.mdl. If you want to learn how to inline your own Fortran MEX-file, see Inlining S-Functions in the Simulink Coder Target Language Compiler documentation .
![]() | Level-1 Versus Level-2 S-Functions | Creating Level-2 Fortran S-Functions | ![]() |

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