In this video, we will cover the basics
on how to use MATLAB system objects.
A system object is a specialized MATLAB object, designed
specifically for implementing and simulating
dynamic systems with inputs that change over time,
such as signal processing, communications,
and control systems algorithms.
MATLAB system objects provide features
that account for tasks such as initialization, execution,
and resetting of an algorithm.
These are some tasks that are usually essential in embedded
systems algorithm design.
They also contain properties, like internal states,
which are necessary for time varying algorithms used
in dynamic simulations, which brings us to Simulink.
Creating MATLAB system objects will
allow you to create MATLAB code that handles states,
provide custom dialog boxes and block ports, and enable access
to two separate simulation modes.
For instance, let's look at the example of a ramp function.
The Simulink provided RAM block is great for implementing
a basic simulation source.
However, if you need an algorithm
with custom initialization or recent behavior,
creating a MATLAB system object will provide a great solution.
So let's walk you through how to get started
creating MATLAB system objects.
In MATLAB you can create a new system object directly
from the Tool strip.
Go to the New Menu dropdown, and select from one
of the System Object options.
In this case, we will start with the basic template.
We will modify the name of our system,
add a property called slope, and another property
corresponding to an internal state
under the discrete state properties.
The three main methods in a system object are set up, step,
and reset.
For a custom ramp function, we will
be initializing our internal state to 0 in the setup method.
Then we will write the main behavior of our algorithm
in the step method.
In this case, that just means returning the current state
and incrementing the state by the slope.
Finally, as part of the reset method,
we will also return the internal state to 0.
Then we will save our system object
with the corresponding name.
To use this algorithm, you can create an object
of this class in MATLAB by assigning the object
constructor to a variable of your choice,
like testRamp, and then calling the object using the step
method.
You will notice that a ramp system object is returning
the expected increasing values.
If you want the ramp to reset the internal states
as programmed, you can call the reset method.
To use this MATLAB system object in Simulink,
place a MATLAB system in your model,
and specify the system object name,
while making sure it is available the MATLAB path.
Then click OK.
When you open the dialog window of your newly created Simulink
block, you will notice that information,
such as the description and relevant properties,
are now available in Simulink as well.
For our purposes, we will connect the scope to the block
output, and run 5 seconds of simulation,
to verify that the ramp output is as expected.
MATLAB system objects are highly customizable,
depending on the algorithm requirements.
You can explore some of the additional methods that
can be implemented in MATLAB system objects
directly through the MATLAB Editor,
or use the Advanced and Simulink Extension templates provided
to explore some of the minimum recommended features.
And if you're interested in learning more
about MATLAB system objects, or other MATLAB features,
please check the documentation links in the description below.