Main Content

Generate Code and Executables for Individual Subsystems

You can generate code and build a standalone executable for a subsystem within a model. When you generate code for an individual subsystem, the code generation and build process uses the code generation and build parameters of the root model.

  1. Open a model with a subsystem, such as rtwdemo_atomic.

    open_system('rtwdemo_atomic')

  2. Open Simulink® Coder™ or Embedded Coder®.

    If you are using Embedded Coder, from the C Code tab, click View Code to open the Code View editor.

  3. Right-click the Subsystem block. From the context menu, click Block Parameters (Subsystem). From the Block Parameters: SS1 window, open the Code Generation tab.

  4. Use the Function name options and File name options parameters to control the names of the generated functions and files. Set the Function name options parameter to Auto so that the code generator uses the name of the subsystem in the generated file names.

  5. Right-click the Subsystem block. From the context menu, select C/C++ Code > Build This Subsystem.

  6. To configure properties such as the name, class, and storage class of variables (or data objects) that are referenced as block parameters in the subsystem, convert the subsystem to a Model block. Configure the properties through the Code Mappings editor.

    For more information on tunable and inlined parameters and storage classes, see Create Tunable Calibration Parameter in the Generated Code.

    The build process displays status messages in the MATLAB® Command Window. When the build is complete, the generated executable is in your working folder. The name of the generated executable is subsystem.exe (on PC platforms) or subsystem (on The Open Group UNIX® platforms). subsystem is the name of the source Subsystem block. In this example, the Subsystem block is named SS1.

    The generated code is in a build subfolder named subsystem_target_rtw. subsystem is the name of the source subsystem block and target is the name of the target configuration.

  7. (Optional) Generate an S-function by right-clicking the Subsystem block and selecting C/C++ Code > Generate S-Function from the context menu. For more information on S-functions, see Generate S-Function from Subsystem.

Subsystem Build Limitations

These limitations apply to building subsystems:

  • A subsystem build does not support a subsystem that has a function-call trigger input or a function-call output.

  • When you build a subsystem that includes an Outport block for which the Data type parameter specifies a bus object, you must address errors that result from setting signal labels. To configure the software to display these errors, in the Configuration Parameters dialog box for the parent model, on the Diagnostics > Connectivity pane, set the Signal label mismatch parameter to error.

  • When a subsystem is in a triggered or function-call subsystem, the right-click build process might fail if the subsystem code is not sample-time independent. To determine whether a subsystem is sample-time independent:

    1. Copy all blocks in the subsystem to an empty model.

    2. In the Configuration Parameters dialog box, on the Solver pane, set:

      1. Type to Fixed-step.

      2. Periodic sample time constraint to Ensure sample time independent.

      3. Click Apply.

    3. Update the model. If the model is sample-time dependent, Simulink generates an error in the process of updating the diagram.

  • When you use the right-click build process for a subsystem, the code generator attempts to use the subsystem name for generated code files. In some cases, there can be a conflict with the name that you specify when you set, for example, File name options to Use function name or Function name options to Use subsystem name. You see an error:

    The subsystem 'model/subsys' 
    is trying to generate code to an reserved file (subsys) for 
    the model 'subsys'...

    To resolve the error, modify one of the conflicting file names so that the names are unique.

  • In a subsystem build warning, the subsystem block path hyperlink that is created references a temporary model block path instead of the actual model block path. In the Diagnostic Viewer, clicking the subsystem hyperlink does not take you to the block. In the Command Window, you see a message:

    ...
    No system or file called 'subsystemName' found.
    ...

Related Examples

More About