Main Content

Convert Subsystem to Referenced Model and Generate Code

Converting subsystems to referenced models enhances incremental code generation, modular development of referenced models, and code reuse. Many large models use a combination of referenced models and subsystems.

To convert a Subsystem block to a Model block, use any of these methods:

  • In the Simulink® Editor, select a Subsystem block. On the Simulink Toolstrip, the Subsystem Block tab, select Convert > Model Block.

  • In the MATLAB® Command Window, use the Simulink.SubSystem.convertToModelReference function with the 'UseConversionAdvisor' argument set to true.

Before you convert a subsystem to a referenced model:

  1. Configure the Subsystem block as an atomic subsystem. In the Subsystem Block Parameters, select the Treat as atomic unit property.

    An atomic subsystem executes as a unit relative to the parent model. Subsystem execution does not interleave with parent block execution. This property makes it possible to extract subsystems for use as standalone models and as functions in the generated code.

  2. Set the model configuration parameter Signal resolution to Explicit only or None.

You can map a storage class to a category of model data elements or to an individual element by using the Code Mappings editor or the Code Mappings API (coder.mapping.api.CodeMapping). Then, use the Code Mappings editor or the API to specify code configurations settings specific for that storage class, such as header files, definition files, and memory sections. If your model contains code mappings, you can migrate the code mappings when you convert a subsystem to a referenced model. For more information, see Code Mappings Editor – C.

Configure Code Mappings

  1. Open the example model RollAxisAutopilot.

    openExample('RollAxisAutopilot')

  2. On the Simulink toolstrip, the Apps tab, select the Embedded Coder or Simulink Coder app.

  3. In the C Code tab, click Code Interface > Default Code Mappings. This option pins the Code Mappings editor.

  4. Map a storage class to a category of model data element. In the Code Mappings editor, select the Data Defaults tab. For the Inports category, specify the Storage Class as ExportedGlobal.

Alternatively, to configure the default mapping for Inports, in the MATLAB Command Window, enter:

roll_mapping = coder.mapping.api.get('RollAxisAutopilot');
setDataDefault(roll_mapping,'Inports','StorageClass','ExportedGlobal');

Convert Subsystem to Referenced Model

  1. To convert a subsystem to a referenced model, open the Model Reference Conversion Advisor. Select the subsystem HeadingMode. On the Subsystem Block tab, select Convert > Model Block.

    The Model Reference Conversion Advisor enables you to interactively specify conversion parameters and fix issues that the advisor finds.

    Model Reference Conversion Advisor window is open. New model name is autopopulated as HeadingMode.

  2. Review the autopopulated model name. If needed, you can update this name.

  3. To make the conversion process faster, select the Fix errors automatically (if possible). This option fixes some conversion issues. You do not control the fixes.

  4. To copy the code mappings specified for the model, select the Copy code mappings option. This option migrates the code mappings from the parent model to the newly converted referenced model. By default, this option is cleared. This option is not supported for models configured with an ERT-based system target file and service code interface.

  5. Click Convert.

    The Model Reference Conversion Advisor steps through each of the checks. Address any issues that the advisor reports. After the Model Reference Conversion Advisor successfully completes the conversion, it replaces the Subsystem block with a Model block that references the new model.

Alternatively, to convert a subsystem to a referenced model, in the MATLAB Command Window, enter:

Simulink.SubSystem.convertToModelReference(...
   'RollAxisAutopilot/HeadingMode', ...
   'HeadingMode', ...
   'AutoFix',true,...
   'ReplaceSubsystem',true,...
   'CopyCodeMappings',true);

Copy Code Mappings After Subsystem Conversion

If you want to copy the code mappings from the parent model to the referenced model and you did not previously select the Copy code mappings option in the Model Reference Conversion Advisor, in the MATLAB Command Window, enter:

roll_mapping = coder.mapping.api.get('RollAxisAutopilot');
roll_sc = getDataDefault(roll_mapping,'Inports','StorageClass');

mode_mapping = coder.mapping.api.get('HeadingMode');
setDataDefault(mode_mapping,'Inports','StorageClass',roll_sc);

Review Generated Code

For the RollAxisAutopilot, model which contains the referenced model HeadingMode, open the C Code tab. To generate code for the model reference hierarchy, click Build.

The Simulink Coder™ build process generates and compiles code. The current folder now contains new files and a new folder.

Folder or FilePurpose
RollAxisAutopilot_grt_rtwThe build folder containing the generated code for the top model.
slprj > grt > Heading ModeThe build folder containing the generated code for the referenced model.
RollAxisAutopilot.exeThe executable created by the build process.
RollAxisAutopilot.slxcThe Simulink cache file for the top model.
HeadingMode.slxcThe Simulink cache file for the referenced model.

Related Topics