Convert Subsystem to Referenced Model
This example shows how you can convert a subsystem to a referenced model by using the Model Reference Conversion Advisor or the Simulink.SubSystem.convertToModelReference
function.
Open the sldemo_mdlref_conversion
model. For the conversion to succeed, the model that contains the subsystem must compile successfully. To compile the model, on the Modeling tab of the Simulink® Toolstrip, click Update Model or Run.
mdl = "sldemo_mdlref_conversion"; open_system(mdl) set_param(mdl,SimulationCommand="Update")
The sldemo_mdlref_conversion
model contains a Subsystem block named Bus Counter
. The Subsystem block represents an atomic subsystem that implements a counter algorithm.
The subsystem interface uses In Bus Element and Out Bus Element blocks to pass a virtual bus into and out of the subsystem. The virtual buses inherit their data types.
To open the Model Reference Conversion Advisor, select a Subsystem block. Then, on the Subsystem Block tab, select Convert > Model Block.
In the Model Reference Conversion Advisor, set New model name to sldemo_bus_counter
, then click Convert.
Alternatively, to convert the subsystem to a referenced model without using the Model Reference Conversion Advisor, enter these commands in the MATLAB® Command Window.
blk = "sldemo_mdlref_conversion/Bus Counter"; newmdl = "sldemo_bus_counter"; Simulink.SubSystem.convertToModelReference(blk,newmdl,... ReplaceSubsystem=true);
### Successfully converted Subsystem block to Model block.
A Model block replaces the Subsystem block. The Model block references the new model named sldemo_bus_counter
.
When possible, the new model uses the same block diagram as the original subsystem.
By default, the conversion does not create Simulink.Bus
objects. To more strongly define the interface of the new model, create bus objects and use them as the data types of the buses. To create bus objects as part of the conversion, use the Simulink.SubSystem.convertToModelReference
function with the CreateBusObjectsForAllBuses
argument set to true
.