Converting Subsystems to Model Reference

This example shows how to automate conversion of a model containing buses to use model reference.

Example Requirements

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

Open the example model. This model contains an atomic subsystem, Bus Counter, modeling a simple counter.

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:

help Simulink.SubSystem.convertToModelReference

at the MATLAB® prompt.

Perform the Conversion

You can run the following command to convert the Bus Counter subsystem to a Model block.

   '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

Close the models and clear variables added to the workspace during this example.

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' when calling Simulink.SubSystem.convertToModelReference.

You can also access the advisor by: # Selecting a Subsystem block. # In the Subsystem Block tab, select Convert > Convert to Model Block.