Today we will show you a MATLAB to Simulink
workflow example We begin with a MATLAB script
and show the steps to convert to a Simulink model.
In most organizations, many teams
may contribute to the same project.
For example, the systems engineering team
adopts model based design workflow.
They create their application in Simulink,
so they can easily transition from a simulation,
to cogeneration, to verification, and even
deployment.
Very frequently, the systems engineering team
gets advanced algorithms from their corporate R&D
team, who uses MATLAB for algorithm development.
The system engineering team needs
to integrate their colleagues' MATLAB algorithm
into their simulation model.
Here, we use a generic application
to illustrate how to convert a MATLAB script into a Simulink
model.
In this example, our application models
target creation, deletion, and visualization.
The targets could be drones flying in round directions
in 3D space.
Moving drones are created, and their trajectories are updated
and plotted every TimeStep.
When they get out of range, we then delete them.
We use a global memory to store the information of all
the live targets.
At the top of the MATLAB script, we
use this global variable, DB, to store
all current live targets, their positions, and velocities.
We then instantiate three system objects
that are used in a for loop.
The system object createRangeBoundedTarget
is used for creating targets.
The system object deleteOutOfRangeTarget
is used for deleting out of range targets.
And the system object viewTargets is
used for viewing live targets.
The global variable, DB, is used for communicating live target
information among these system objects.
To make the application more interesting,
we have two MATLAB packages for defining targets
that move at different speeds.
MATLAB packages are special folders
that can contain class folders, functions, and class definition
files, and other packages.
The names of classes and functions
are scoped to the package folder.
This for loop is the main MATLAB implementation.
Inside, we create targets, delete out of range targets,
and visualize all live targets.
After that, all live target locations
are updated with the MATLAB function
updateAllTargetPositions.
We can now see the code in action, creating and destroying
drones as they fly outside the bounds that we've defined.
Now, we convert our MATLAB script to a Simulink model.
To do this, we map our MATLAB constructs
into Simulink blocks.
The global, DB, is mapped onto the data store memory
block, DB.
This MATLAB system block Target Creator
is equivalent to the system object createRangeBoundTarget
in MATLAB.
MATLAB system block Target Deleter
is equivalent to the system object deleteOutOfRangeTarget
in MATLAB.
The Simulink Display block is used to show the deleted target
at any given time.
The MATLAB system block Target Viewer
is equivalent to the system object viewTargets in MATLAB.
The MATLAB function updateAllTargetPositions
is implemented using the MATLAB function block Update Targets.
And finally, we use Simulink functions
to provide the same scoping abilities as the MATLAB
packages we see before.
Since the Simulink model runs naturally in a loop,
the model stop time dictates the number of [? TimeSteps ?]
to be run.
This is equivalent to using the for loop in our previous MATLAB
script.
Once again, we can run the simulation.
And we can see that we've recreated
the behavior using Simulink.
This example is shipped in 22a.
Our documentation also provides a very useful table
for mapping MATLAB constructs to Simulink constructs,
so you can perform this mapping yourself in the future.