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:
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.
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 Property Inspector 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.
Open the example model
rtwdemo_roll
.
rtwdemo_roll
On the Simulink toolstrip, the Apps tab, select the Embedded Coder or Simulink Coder app.
In the C Code tab, click Code Interface > Default Code Mappings. This option pins the Code Mappings editor and the Property Inspector pane.
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('rtwdemo_roll'); setDataDefault(roll_mapping,'Inports','StorageClass','ExportedGlobal');
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.
Review the autopopulated model name. If needed, you can update this name.
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.
To copy the code mappings specified for the model, select the Copy code mapping information to the new converted model. This option migrates the code mappings from the parent model to the newly converted referenced model. By default, this option is cleared.
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(... 'rtwdemo_roll/HeadingMode', ... 'HeadingMode', ... 'AutoFix',true,... 'ReplaceSubsystem',true,... 'CopyCodeMappings',true);
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 mapping information to the new converted model option in the Model Reference Conversion Advisor, in the MATLAB Command Window, enter:
roll_mapping = coder.mapping.api.get('rtwdemo_roll'); roll_sc = getDataDefault(roll_mapping,'Inports','StorageClass'); mode_mapping = coder.mapping.api.get('HeadingMode'); setDataDefault(mode_mapping,'Inports','StorageClass',roll_sc);
For the rtwdemo_roll
, 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 File | Purpose |
---|---|
rtwdemo_roll_grt_rtw | The build folder containing the generated code for the top model. |
slprj > grt >
Heading Mode | The build folder containing the generated code for the referenced model. |
rtwdemo_roll.exe | The executable created by the build process. |
rtwdemo_roll.slxc | The Simulink cache file for the top model. |
HeadingMode.slxc | The Simulink cache file for the referenced model. |