Generate Code and Executables for Individual Subsystems

You can generate code and build an executable for a subsystem within a model. The code generation and build process uses the code generation and build parameters of the root model.

  1. In the Configuration Parameters dialog box, set up the code generation and build parameters, similar to setting up the code generation for a model.

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

    Alternatively, in the current model, click a subsystem and then from the C Code tab, click Build.

    Note

    When you select Build This Subsystem, if the model is operating in external mode, the build process automatically turns off external mode for the duration of the build. The code generator restores external mode upon completion of the build process.

  3. The Build code for Subsystem window displays a list of the subsystem parameters. The upper pane displays the name, class, and storage class of each variable (or data object) that is referenced as a block parameter in the subsystem. When you select a parameter in the upper pane, the lower pane shows the blocks that reference the parameter and the parent system of each block.

    The Storage Class column contains a menu for each row. The menu options set the storage class or inline the parameter. To declare a parameter to be tunable, set the Storage Class to a value other than Inlined.

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

  4. After selecting tunable parameters, click Build to initiate the code generation and build process.

  5. 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.

    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.

When you generate code for a subsystem, you can generate an S-function by right-clicking the Subsystem block and selecting C/C++ Code > Build This Subsystem from the context menu. For more information on S-functions, see Generate S-Function from Subsystem.

Subsystem Build Limitations

The following limitations apply to building subsystems:

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

  • When you right-click a Subsystem block and select C/C++ Code > Build This Subsystem from the context menu to 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 find out 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.
    ...