How can I create an array in Stateflow in the form Arr= [x y z]?

I use normal stateflow chart (not Chart(MATLAB))in Simulink R2012b.
I want to create an array of ten elements in Stateflow of the form:
P=[P1 P2 P3 P4 P5 P6 P7 P8 P9 P10].
Each element of the P-array is read in as an input from Simulink and can change in value during the simulation. P itself is an output to Simulink that I have set up to have a size of 10.
I have tried the method above, tried
P={P1 P2 P3 P4 P5 P6 P7 P8 P9 P10} and
P=(P1 P2 P3 P4 P5 P6 P7 P8 P9 P10)
but to no avail.
I don't want to do P[0]=P1;P[1]=P2... as it will be too long for me and besides, it seems to output my result as a scalar and not a 10x1 vector when I do so.
I have also tried to use the ml function such that P is defined as
P=ml('[P1 P2 P3 P4 P5 P6 P7 P8 P9 P10]')
with the following pre-definitions: ml.P1=P1, ml.P2=P2.... This too returns an error: 'Eval of MATLAB expression '[P1 P2 P3 P4 P5 P6 P7 P8 P9 P10]' in base workspace didn't return anything...'
I know this is straightforward in Matlab but I don't understand why it is so difficult in Stateflow.
Anyone who has addressed a similar challenge before should pls help. Any help will be greatly appreciated.
Thanks.

Answers (2)

You can use
P=[P1 P2 P3 P4 P5 P6 P7 P8 P9 P10].
% P=(P1 P2 P3 P4 P5 P6 P7 P8 P9 P10) is not valid
Or you can use a MUX block in simulink, you will pass through it P1,P2,... then your Stateflow chart will have one input P with size (1,10). If you need P3 in your Stateflow chart, just use P[2].

3 Comments

Thanks Azzi, but when I do
P=[P1 P2 P3 P4 P5 P6 P7 P8 P9 P10] in stateflow either as a chart entry action or as a transition action
I get a Lex Error:
P= [P1 P2 P3 P4 P5 P6 P7 P8 P9 P10]
^
syntax error.
As for the second suggestion, the array is meant to be formed from data calculated in stateflow and sent as an output to simulink, so I think the MUX method may not work.
Is there another way of doing it?
In your simulink model use a MUX block to have one signal P_in with size 10. Then your stateflow chart will have one input with size 10. In you chart click on your model explorer, set the size of your input P_in and output P_out to 10. If you want to use P1, just use P_in[0], for P2, it's P_in[1], and so on.
To output your signal just use
P_out=P_in
Thanks Azzi! This works. It is quite awkward though as I have to assign values to P[0], P[1],P[2]...leading to very log expressions.

Sign in to comment.

In R2012b, the MATLAB Chart was introduce. In this release your initial attempt should work.

Categories

Asked:

on 13 Jan 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!