To generate code for referenced models, you
Create a subsystem in an existing model.
Convert the subsystem to a referenced model (Model block).
Call the referenced model from the top model.
Generate code for the top model and referenced model.
Explore the generated code and the code generation folder.
You can accomplish some of these tasks automatically with a function called
Simulink.Subsystem.convertToModelReference
.
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
).
In the MATLAB® Command Window, create a new working folder wherever you want
to work and cd
into it:
mkdir mrexample cd mrexample
Open the vdp
example model by
typing:
vdp
Drag a box around the three blocks outlined in blue below:
Choose Create Subsystem from the Multiple tab on the toolstrip.
A subsystem block replaces the selected blocks.
If the new subsystem block is not where you want it, move it to a preferred location.
Rename the block vdpmult
.
Right-click the vdpmult
block and select
Block Parameters (Subsystem).
The Block Parameters dialog box appears.
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:
Open Model Explorer. On the Modeling tab, click Model Explorer.
In the Model Hierarchy pane, expand the model to reveal its components.
Select the Configurations node.
In the Contents pane, right-click Configuration (Active) and click Open in the context menu to open the Configuration Parameters dialog box.
In the left pane of the Configuration Parameters dialog box, select Solver.
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.
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
.
Click Apply.
The model now has the properties that model referencing requires.
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.
In the vdp
model window, choose File > Save as. Save the model as vdptop
in your working
folder. Leave the model open.
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).
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:
Extracts the subsystem vdpmult
from
vdptop
.
Converts the extracted subsystem to a separate model named
vdpmultRM
and saves the model to the working
folder.
In vdptop
, replaces the extracted subsystem with a
Model block that references vdpmultRM
.
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:
The files in your working folder now consist of the following (not in this order).
File | Description |
---|---|
| Top model that contains a Model
block where the |
| Referenced model created for the
|
| Static library file (Microsoft®
Windows® platforms only). The file extension is
system-dependent and may differ. This file executes when
the |
| 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.
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:
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:
Verify that you are still working in the
mrexample
folder.
If the model vdptop
is not open,
open it. Make sure it is the active window.
Open Model Explorer. On the Modeling tab, click Model Explorer.
In the Model Hierarchy pane, click
the symbol preceding the vdptop
model to reveal its
components.
Select the Configurations node below the model node.
In the Contents pane, right-click Configuration (Active) and click Open in the context menu to open the Configuration Parameters dialog box.
In the left pane, select Data Import/Export.
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.
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.
File | Description |
---|---|
| The executable created by the build process |
| 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
:
Open Model Explorer. On the Modeling tab, click Model Explorer.
In the Model Hierarchy pane, expand the model name to reveal its components.
Expand Code for vdptop
to reveal its components.
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.
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.