mdlref_conversion.mdl | Open this Model |
Converting Existing Models to Using Model Reference Demonstration
This demonstration shows one way to automate the conversion of an existing model with buses to using model reference. (For a quick introduction to Model Reference, run the Basic Model Reference Demo and for a quick introduction to bus signals, run the Bus Signal Demo.)
During this demonstration, Simulink will generate code in a Simulink project directory created in the current working directory. If you do not want to (or if you cannot) generate files in this directory, you should change the working directory to a suitable directory.
If you plan to alter the demonstration models or functions:
Preserve the demo in its original state by copying the following files in your MATLAB installation directory without changing their names to a different directory:
Change your current working directory to the directory to which you copied the files.
Continue with the demonstration.
This demonstration function is: sl_convert_to_model_reference. It has two required inputs, the name of the model to convert and the directory in which to save the converted model.
This function looks for Atomic Subsystems in the model and creates stand alone models from them. These created models will have all of the same compiled properties (for example sample times, data types, dimensions) as the original subsystem. The Subsystem in the original model is then replaced with a Model Block. Additionally, any buses going into or out of the subsystem will have bus objects created for them and set on the bus creators and inport and outport blocks.
If you would like to use this demonstration function as a starting point for converting your own models, you will also probably be interested in the four helper functions:If you haven't already opened it, open the demo model mdlref_conversion.
This model contains a Bus Creator block, COUNTERBUSCreator, that drives an atomic subsystem Bus Counter, modeling a simple counter. A bus signal called COUNTERBUS feeds the input signal and the saturation limits of the counter into the subsystem. The COUNTERBUS contains two more bus signals, the LIMITBUS and the SIGNALBUS. LIMITBUS contains the upper and lower limit values of the counter and SIGNALBUS contains the signal used in counting. The following shows the structure of COUNTERBUS:
Note that in the mdlref_conversion demonstration model the "Specify properties via bus object" checkbox is unchecked on all three of the Bus Creator Blocks above.
Note also that the Inport of the subsystem Bus Counter, does not specify any of its signal properties on the Signal specification tab of its parameter dialog, nor does the Outport.
Now you can convert
mdlref_conversion
.
To try this demonstration function on another model you will have to use sl_convert_to_model_reference
at the command line.
The new model, mdlref_conversion_converted, contains a Model block, Bus Counter, that references Bus_Counter, a model that contains the contents of the original subsystem Bus Counter, in the original model mdlref_conversion.
Comparing the original model, mdlref_conversion with the newly created model, mdlref_conversion_converted, there are several differences worth noting:
Note: You can view these bus objects by selecting the MATLAB workspace in the Model Explorer. You can also view the bus structures in the Bus Types Editor.