Fortran S-Functions

The Simulink® Real-Time™ product supports Fortran in Simulink models using S-functions. For more details, see Create Level-2 Fortran S-Functions (Simulink) and Port Legacy Code (Simulink).


You must have Simulink Real-Time Version 1.3 or later to use Fortran for real-time applications. The Simulink Real-Time product supports the Fortran compilers listed here:

Simulink S-Function Example

The Simulink examples folder contains a tutorial and description on how to incorporate Fortran code into a Simulink model using S-functions. To access the tutorial and description:

  1. Open Custom Code and Hand Coded Blocks using the S-function API (Simulink)

  2. Open the associated model.

  3. Open the Fortran S-functions example model. Fortran S-functions and associated templates appear.

Steps to Incorporate Fortran

This topic lists the general steps to incorporate Fortran code into a real-time application. Detailed commands follow in the accompanying examples.

  1. Using the Fortran compiler, compile the Fortran subroutines (*.f). Specify particular compiler options.

  2. Write a Simulink C-MEX wrapper S-function. This wrapper S-function calls one or more of the Fortran subroutines in the compiled Fortran object code from step 1.

  3. Use the mex function to compile this C-MEX S-function using a Microsoft® Visual C++® compiler. Define several Fortran run-time libraries to be linked in.

    This step creates the Simulink S-function MEX-file.

  4. To validate the compiled Fortran code and wrapper S-function, run a simulation C-MEX file with the Simulink software.

  5. Copy the relevant Fortran run-time libraries to the real-time application build folder.

  6. Define the Fortran libraries, and the Fortran object files from step 1, in the Simulink Coder™ dialog box of the Simulink model. Define these libraries and files as additional components to be linked in when the real-time application link takes place.

  7. Initiate the Simulink Real-Time specific Simulink Coder build procedure for the example model. Simulink Coder builds and downloads Simulink Real-Time onto the target computer.

Related Topics