Convert MATLAB Algorithms of Dynamic Systems into a Simulink Model
This example shows how to convert MATLAB® code that models a dynamic system using System objects into a Simulink® model. This example maps the dynamic system components defined in MATLAB onto a Simulink block diagram using blocks with similar functionality. Transforming MATLAB code to a Simulink model enhances your modeling experience by allowing you to create a block diagram of the system, simulate the model, visualize the results via graphs and animation, log data, verify and validate, and generate code.
Model Dynamic System Using MATLAB Code
Start by reviewing the MATLAB code TargetIllustrationScript
that represents a dynamic system.
open('TargetIllustrationScript.m')
TargetIllustrationScript.m
creates, moves, deletes, updates, and helps to visualize targets. The target creator, the target deleter, and the target viewer are defined using System objects that use internal states to store past behavior and use it in the next computational step.
The target database
DB
is a global database. TheinitDB
function initializes the global database and creates the target struct.The target creator,
TargetCreator.m
uses two different MATLAB namespacesTargetCreationInfoNamespace1
andTargetCreationInfoNamespace2
, to create the targets. Subsequently, the target deleter,TargetDeleter.m
checks and deletes any out-of-range targets.The target updater updates the remaining targets using the
updateAllTargetPositions
function.The target viewer,
TargetViewer.m
enables you to visualize the targets.
For this example, the target range is 5. The system deletes a target greater than 5 and updates the remaining target positions. This process loops over each time step for approximately 200 s. Run the script and observe the process.
TargetIllustrationScript;
Convert Code to Simulink Model
Open the Simulink model slexTargetIllustrationModel.slx
that has similar functionality as the MATLAB code TargetIllustrationScript.m
. Mapping this code onto a Simulink model further enhances the modeling experience by allowing you to create a block diagram of the system and visualize the simulation results via graphs and animation.
mdl = 'slexTargetIllustrationModel.slx';
open_system (mdl)
In the model:
A Data Store Memory block defines the target database
DB
. ASimulink.Bus.createObject
object defines the target struct using the model post load callback function. To access the function, on the Modeling tab, choose Model Properties from Model Settings drop-down options. In the Model Properties dialog box, in the Callbacks section, select PostLoadFcn to access the function.MATLAB System block
TargetCreator
maps the target creator onto the Simulink model. The functions inTargetCreationInfoNamespace1
andTargetCreationInfoNamespace2
are defined using scoped Simulink Function blocks and called by theTargetCreator
. TheTargetCreator
usesgetGlobalNamesImpl
andgetSimulinkFunctionNamesImpl
methods to declare the function names.MATLAB System blocks
TargetDeleter
andTargetViewer
mapTargetDeleter.m
andTargetViewer.m
System objects onto the Simulink model, respectively.A MATLAB Function block
updateTargets
mapsupdateAllTargetPositions.m
function onto the model. The number of time steps in the loop corresponds to the stop time in the toolbar.
Simulate the model and visualize the results.
out = sim(mdl);
See Also
MATLAB System | Simulink Function | MATLAB Function | Data Store Memory | Chart (Stateflow)