This example shows how to automate conversion of a model containing buses to use model reference.
During this example, Simulink® generates files in the current working directory. If you do not want to generate files in this directory, you should change the working directory to a suitable directory.
Open the Example Model
Convert a Subsystem to Model Reference
You can use
Simulink.SubSystem.convertToModelReference to convert an atomic subsystem to model reference. This utility function converts an atomic subsystem by creating a new model, copying the contents of the subsystem into the model, and reconfiguring the root level Inport and Outport blocks and configuration parameters of the model. Then, based on its input arguments, it either replaces the subsystem block with a Model block that references the new model, or it creates another, temporary model containing a Model block that references the model derived from the subsystem block. For more information see Simulink documentation or type:
at the MATLAB® prompt.
Perform the Conversion
Simulink.SubSystem.convertToModelReference(... 'sldemo_mdlref_conversion/Bus Counter', ... 'sldemo_bus_counter', ... 'ReplaceSubsystem', true);
Simulate and Generate Code from the Model
You can update, simulate, and generate code for the model containing the Model block.
Export Simulation Data
You can log time, outputs, states, and final states data of the simulation. The logging is configured to use Dataset format.
Close Models and Clear Workspace
Model Reference Conversion Advisor
You can use the Model Reference Conversion Advisor to interactively perform the conversion. To do so, set the 'UseConversionAdvisor' parameter to 'true'.
You can also access the advisor by right-clicking a Subsystem block and selecting Subsystems & Model Reference > Convert Subsystem to > Model Reference.