Convert MATLAB Code into Stateflow Flow Charts - MATLAB & Simulink (original) (raw)

Main Content

To transform your MATLAB® code into Stateflow® flow charts and graphical functions, use the Pattern Wizard. Supported patterns for conversion include:

The Pattern Wizard can convert MATLAB functions and scripts.

Converting MATLAB code is supported only in standalone Stateflow charts. For more information, see Create Stateflow Charts for Execution as MATLAB Objects.

Create Flow Charts from MATLAB Scripts

This MATLAB script empirically verifies one instance of the Collatz conjecture. When given the numeric input u, the script computes the hailstone sequence _n_0 = u,_n_1,_n_2,_n_3, ⋯ by iterating this rule:

The Collatz conjecture states that every positive integer has a hailstone sequence that eventually reaches one.

% Hailstone sequence u, c(u), c(c(u)),... y = u; while y(end) ~= 1 y(end+1) = c(y(end)); end disp(y);

function n = c(n) % Compute next number in hailstone sequence. % If n is even, then c(n) = n/2. % If n is odd, then c(n) = 3n+1. if rem(n,2) == 0 n = n/2; else n = 3n+1; end end

The script executes a while loop that repeatedly calls the auxiliary function c until it produces an output value of one. The functionc consists of a conditionalif-else statement whose output depends on the parity of the input.

To convert this script into a flow chart and a graphical function:

  1. Open a new standalone chart.
  2. On the State Chart tab, selectPattern > Select File.
  3. In the dialog box, choose the MATLAB script and click Open. The Pattern Wizard adds a flow chart and a graphical function to your Stateflow chart. Double-click the graphical function to see its contents.
    Flow chart that automatically generated from a MATLAB script.
  4. In the Symbols pane, click Resolve Undefined Symbols . The Stateflow Editor resolves u and y as local data.
  5. Save your chart.
  6. To execute the chart from the Stateflow Editor, in the Symbols pane, enter a value of u = 9 and click Run . While the flow chart is executing, the Stateflow Editor highlights the active transitions through chart animation. When the execution stops, the MATLAB Command Window displays the hailstone sequence, starting with a value of nine:
    9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
  7. Click Stop .

You can copy generated flow charts and graphical functions and paste them in other charts, including Stateflow charts in Simulink® models. If your MATLAB code uses functionality that is restricted for code generation in Simulink, you must modify the flow chart actions before simulating the chart. For more information, see Call Extrinsic MATLAB Functions in Stateflow Charts.

Note

Suppose that you use nargin in a MATLAB function that you convert to a graphical function in a chart. Becausenargin counts the chart object as one of the input arguments of the graphical function, the value of nargin in the graphical function is equal to one plus the value of nargin in the original MATLAB function. For more information, see Execute a Standalone Chart.

See Also

Topics