Guy and Seth on Simulink

February 28th, 2013

MATLAB Language in Stateflow

Have you noticed a new block in the Stateflow Library since R2012b?

Chart (MATLAB)

The MATLAB Chart block uses MATLAB as the action language for states and transitions in Stateflow.

This means that you don't have to create a separate MATLAB Function every time you want to do matrix math in a Stateflow chart. Let's look at a few examples of what can be done in a MATLAB Chart.

Matrix Operations

Let's say I define a bus object containing a 3x1 position vector and a 3x3 rotation matrix:

Bus Object

With MATLAB as the action language, I can multiply the bus elements directly in a transition action:

matrix multiplication in stateflow

Access to all MATLAB Coder supported functions

With MATLAB as the action language, you can use any function supported by MATLAB Coder for code generation. For example, I can use the functions like ones and eig in a transition:

Using MATLAB Functions in transition actions

Manipulating Arrays of buses

Let's say I want my chart to generate an array of the bus defined previously, I could write something like:

Manipulating arrays of buses in Stateflow

Conclusion

Based on my experience, using MATLAB as the action language in Stateflow charts makes it a lot easier to implement the algorithms I need. Since I first tried it, I have not needed to use C as the action language.

Of course, the MATLAB Chart block is not the best choice for all applications. If your application is interfacing with legacy C functions or more naturally expressed in C language, than there is no need to switch. The C chart block that we are all used to still continues to be developed and supported.

If you need help deciding which block to use, I recommend looking at the Differences Between MATLAB and C as Action Language Syntax in the Stateflow documentation.

Now it's your turn

Have you tried using MATLAB as the action language in your Stateflow charts? Let us know what you think by leaving a comment here.

2 Responses to “MATLAB Language in Stateflow”

  1. Nadeer replied on :

    Hi Guy,
    When I use chart(MATLAB), will there be any issue if I export the model to a previous version ?
    Rgds

  2. Guy Rouleau replied on :

    @Nadeer: If you export a model with a MATLAB Chart to a previous version, you will receive a warning because MATLAB Chart did not exist in previous releases.

    Simulink will save the chart using the C action language and you will very likely need to edit the actions to comply with the C language.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks
Guy Rouleau and Seth Popinchalk are Application Engineers for MathWorks. They write here about Simulink and other MathWorks tools used in Model-Based Design.

These postings are the author's and don't necessarily represent the opinions of MathWorks.