Generate Code for Referenced Models

About Generating Code for Referenced Models

To generate code for referenced models, you

  1. Create a subsystem in an existing model.

  2. Convert the subsystem to a referenced model (Model block).

  3. Call the referenced model from the top model.

  4. Generate code for the top model and referenced model.

  5. Explore the generated code and the code generation folder.

You can accomplish some of these tasks automatically with a function called Simulink.Subsystem.convertToModelReference.

Create and Configure the Subsystem

In the first part of this example, you define a subsystem for the vdp example model, set configuration parameters for the model, and use the Simulink.Subsystem.convertToModelReference function to convert it into two new models — the top model (vdptop) and a referenced model vdpmultRM containing a subsystem you created (vdpmult).

  1. In the MATLAB® Command Window, create a new working folder wherever you want to work and cd into it:

    mkdir mrexample
    cd mrexample
  2. Open the vdp example model by typing:

    vdp
  3. Drag a box around the three blocks outlined in blue below:

  4. Choose Create Subsystem from the Multiple tab on the toolstrip.

    A subsystem block replaces the selected blocks.

  5. If the new subsystem block is not where you want it, move it to a preferred location.

  6. Rename the block vdpmult.

  7. Right-click the vdpmult block and select Block Parameters (Subsystem).

    The Block Parameters dialog box appears.

  8. In the Block Parameters dialog box, select Treat as atomic unit, then click OK.

    The border of the vdpmult subsystem thickens to indicate that it is now atomic. An atomic subsystem executes as a unit relative to the parent model: subsystem block execution does not interleave with parent block execution. This property makes it possible to extract subsystems for use as stand-alone models and as functions in generated code.

    The block diagram appears.

You must set several properties before you can extract a subsystem for use as a referenced model. To set the properties:

  1. Open Model Explorer. On the Modeling tab, click Model Explorer.

  2. In the Model Hierarchy pane, expand the model to reveal its components.

  3. Select the Configurations node.

  4. In the Contents pane, right-click Configuration (Active) and click Open in the context menu to open the Configuration Parameters dialog box.

  5. In the left pane of the Configuration Parameters dialog box, select Solver.

  6. Change the Type to Fixed-step, then click Apply. You must use fixed-step solvers when generating code, although referenced models can use different solvers than top models.

  7. In the left pane, click the symbol preceding Diagnostics. In the left pane below Diagnostics, select Data Validity. In the Signals area, set Signal resolution to Explicit only. Alternatively, if you do not want to use Simulink.Signal objects, set Signal resolution to None.

  8. Click Apply.

    The model now has the properties that model referencing requires.

  9. In the left pane, click Model Referencing. In the Options for all referenced models section, set Rebuild to If any changes in known dependencies detected. Click OK. This setting prevents code regeneration when it is not required.

  10. In the vdp model window, choose File > Save as. Save the model as vdptop in your working folder. Leave the model open.

Convert Model to Use Model Referencing

In this portion of the example, you use the conversion function Simulink.SubSystem.convertToModelReference to extract the subsystem vdpmult from vdptop and convert vdpmult into a referenced model named vdpmultRM. To see the complete syntax of the conversion function, type at the MATLAB prompt:

help Simulink.SubSystem.convertToModelReference

For additional information, type:

doc Simulink.SubSystem.convertToModelReference

If you want to see an example of Simulink.SubSystem.convertToModelReference before using it yourself, type:

sldemo_mdlref_conversion

Simulink® also provides a menu command, Subsystem & Model Reference > Convert Subsystem to > Referenced Model, that you can use to convert a subsystem to a referenced model. The command calls Simulink.SubSystem.convertToModelReference with default arguments. For more information, see Convert Subsystems to Referenced Models (Simulink).

Extract the Subsystem to a Referenced Model

To use Simulink.SubSystem.convertToModelReference to extract vdpmult and convert it to a referenced model, type:

Simulink.SubSystem.convertToModelReference...
('vdptop/vdpmult', 'vdpmultRM',...
'ReplaceSubsystem', true, 'BuildTarget', 'Sim')

This command:

  1. Extracts the subsystem vdpmult from vdptop.

  2. Converts the extracted subsystem to a separate model named vdpmultRM and saves the model to the working folder.

  3. In vdptop, replaces the extracted subsystem with a Model block that references vdpmultRM.

  4. Creates a simulation target for vdptop and vdpmultRM.

The converter prints progress messages and terminates with

ans =
     1

The parent model vdptop now looks like this:

Note the changes in the appearance of the block vdpmult. These changes indicate that it is now a Model block rather than a subsystem. As a Model block, it does not have contents of its own: the previous contents now exist in the referenced model vdpmultRM, whose name appears at the top of the Model block. Widen the Model block to expose the complete name of the referenced model.

If the parent model vdptop had been closed at the time of conversion, the converter would have opened it. Extracting a subsystem to a referenced model does not automatically create or change a saved copy of the parent model. To preserve the changes to the parent model, save vdptop.

Right-click the Model block vdpmultRM and choose Open to open the referenced model. The model looks like this:

Files Created and Changed by the Converter

The files in your working folder now consist of the following (not in this order).

File Description

vdptop model file

Top model that contains a Model block where the vdpmult subsystem was

vdpmultRM model file

Referenced model created for the vdpmult subsystem

vdpmultRM_msf.mexw64

Static library file (Microsoft® Windows® platforms only). The file extension is system-dependent and may differ. This file executes when the vdptop model calls the Model block vdpmult. When called, vdpmult in turn calls the referenced model vdpmultRM.

/slprj

Folder for generated model reference code

Code for model reference simulation targets is placed in the slprj/sim subfolder. Generated code for GRT, ERT, and other Simulink Coder™ targets is placed in slprj subfolders named for those targets. You will inspect some model reference code later in this example. For more information on code generation folders, see Work with Code Generation Folders.

Run the Converted Model

Open the Scope block in vdptop if it is not visible. In the vdptop window, click the Run tool or click Run from the Simulation tab. The model calls the vdpmultRM_msf simulation target to simulate. The output looks like this:

Generate Model Reference Code for a GRT Target

The function Simulink.SubSystem.convertToModelReference created the model and the simulation target files for the referenced model vdpmultRM. In this part of the example, you generate code for that model and the vdptop model, and run the executable you create:

  1. Verify that you are still working in the mrexample folder.

  2. If the model vdptop is not open, open it. Make sure it is the active window.

  3. Open Model Explorer. On the Modeling tab, click Model Explorer.

  4. In the Model Hierarchy pane, click the symbol preceding the vdptop model to reveal its components.

  5. Select the Configurations node below the model node.

  6. In the Contents pane, right-click Configuration (Active) and click Open in the context menu to open the Configuration Parameters dialog box.

  7. In the left pane, select Data Import/Export.

  8. In the Save to workspace or file section, select Time and Output and clear Data stores. Click Apply.

    These settings instruct the model vdptop (and later its executable) to log time and output data to MAT-files for each time step.

  9. Generate GRT code (the default) and an executable for the top model and the referenced model. For example, in the model, press Ctrl+B.

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

FileDescription

vdptop.exe

The executable created by the build process

vdptop_grt_rtw/

The build folder, containing generated code for the top model

The build process also generated GRT code for the referenced model and placed it in the slprj folder.

To view a model’s generated code in the Model Explorer, the model must be open. To use the Model Explorer to inspect the newly created build folder, vdptop_grt_rtw:

  1. Open Model Explorer. On the Modeling tab, click Model Explorer.

  2. In the Model Hierarchy pane, expand the model name to reveal its components.

  3. Expand Code for vdptop to reveal its components.

  4. Click This Model.

    A list of generated code files for vdptop appears in the Contents pane:

    rtmodel.h
    vdptop.c
    vdptop.h
    vdptop.mk
    vdptop_private.h
    vdptop_types.h

    You can browse code by selecting a file of interest in the Contents pane.

    To open a file in a text editor, click a filename, and then click the hyperlink that appears in the gray area at the top of the Document pane. This figure shows viewing code for vdptop.c in a text editor. Your code may differ.

    To view the generated code in the code generation report, see Generate a Code Generation Report.

Work with Code Generation Folders

Model reference code is generated in your code generation folder (Simulink) and simulation target code is generated in your simulation cache folder (Simulink). Because of this process, there are constraints on:

  • When and where model reference targets are built.

  • How the model reference targets are accessed.

The models referenced by Model blocks can be stored anywhere on the MATLAB path. A given top model can include models stored on different file systems or in different folders. The same is not true for the simulation targets and generated code derived from these models. Under most circumstances, to allow code reuse, models referenced by a given top model must be set up to simulate and generate model reference target code in a single code generation folder.

This means that, if you reference the same model from several top models, each stored in a different folder, you must choose one of these approaches:

  • Always work with the same code generation folder and be sure that the models are on your path.

  • Allow separate code generation folders, simulation targets, and Simulink Coder targets to be generated in each folder in which you work.

The second approach requires maintenance of several instances of the model reference code and it is possible for generated code to become redundant. For example, when you make changes to the referenced model. Therefore, to minimize code regeneration of referenced models, choose a specific code generation folder for all sessions.

Related Topics