Main Content

Export Simulink Model to Standalone FMU

This example shows how to export Simulink® component to standalone Co-Simulation FMU 3.0 with Simulink Compiler™. For a detailed explanation of the model, see:

In this example, the air-fuel ratio control system is composed of three Simulink models:

  • Fuel Rate Control Component: fmudemo_export_fuelsys_controller,

  • Engine Gas Dynamics Component: fmudemo_export_fuelsys_plant, and

  • top-level model fmudemo_export_fuelsys_top.

Once the controller and plant components are exported to FMU format, they can be integrated using the top-level model. The generated FMUs can also be imported into other simulation tools that support FMI. For a list of Tools that support FMI, see: https://fmi-standard.org/tools/.

Export Fuel Rate Control Component to FMU

Open the fmudemo_export_fuelsys_controller example model.

From Simulation tab, click drop-down button for Save. In Export Model To section, click Standalone FMU.... In FMU Export dialog box, configure wrapper model and icon settings, and specify save location for generated FMU.

Click Create to export to FMU. The fmudemo_export_fuelsys_controller.fmu file can be found at specified save location.

Setting System Target to FMU 'Co-Simulation' for model 'fmudemo_export_fuelsys_controller'.
Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'fmudemo_export_fuelsys_controller'.
Warning: Root Port 'sensors' has a child element 'throttle' with datatype
's16En3', which cannot be configured for FMU export. To specify bus or
enumeration types, use prefix 'Bus: ' or 'Enum: '. 
### Searching for referenced models in model 'fmudemo_export_fuelsys_controller'.
### Total of 1 models to build.
### 'GenerateComments' is disabled for 'Co-Simulation' FMU Export.
Warning: Parameter 'st_range' present in 'base workspace' and used as
expression in
'fmudemo_export_fuelsys_controller/validate_sample_time/CheckRange' is not
exported. Convert the expression to a parameter to export it. For more
information, see <a
href="matlab:helpview('slcompiler','FMUParamNotExported')">Limitations for
Parameter Tunability in Generated FMU</a>. 

Build Summary

Top model targets:

Model                              Build Reason                                         Status                        Build Duration
====================================================================================================================================
fmudemo_export_fuelsys_controller  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 10.193s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 10.697s
### Model was successfully exported to 'Co-Simulation' FMU 'fmudemo_export_fuelsys_controller.fmu' in '/tmp/Bdoc26a_3163660_733221/tpd2a2f051/fmubuilder-ex60485454'.

Export Engine Gas Dynamics Component to FMU

Open the fmudemo_export_fuelsys_plant example model.

FMU can also be exported using command-line. In MATLAB® command line window, use exportToFMU command:

% Export model to Standalone Co-Simulation FMU 3.0
exportToFMU('fmudemo_export_fuelsys_plant', 'FMIVersion', '3.0', 'FMUType', 'CS', 'CreateModelAfterGeneratingFMU', 'off', 'AddIcon', 'snapshot', 'SaveDirectory', pwd);
Setting System Target to FMU 'Co-Simulation' for model 'fmudemo_export_fuelsys_plant'.
Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'fmudemo_export_fuelsys_plant'.
### Searching for referenced models in model 'fmudemo_export_fuelsys_plant'.
### Total of 1 models to build.
### 'GenerateComments' is disabled for 'Co-Simulation' FMU Export.
Warning: The following variables: 'engine speed,throttle angle,air/fuel ratio'
are not unique, too long, or contain invalid characters. They will be renamed
to: 'engineSpeed,throttleAngle,air_fuelRatio'. 

Build Summary

Top model targets:

Model                         Build Reason                                         Status                        Build Duration
===============================================================================================================================
fmudemo_export_fuelsys_plant  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 8.965s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 9.1631s
### Model was successfully exported to 'Co-Simulation' FMU 'fmudemo_export_fuelsys_plant.fmu' in '/tmp/Bdoc26a_3163660_733221/tpd2a2f051/fmubuilder-ex60485454'.

You can use optional arguments CreateModelAfterGeneratingFMU, AddIcon, and SaveDirectory to configure FMU export settings. For more information, call help ExportToFMU.

Integrate FMU Components in Simulink

Once both FMUs are successfully exported, you may use the top model fmudemo_export_fuelsys_top to fully integrate the system for testing.