# Guy and Seth on Simulink

## MATLAB Language in Stateflow

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

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:

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

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:

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:

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.

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”

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.

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 1 + 4 ?

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).

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.