Main Content

Log String Data to the Simulation Data Inspector

This example shows how to build a Stateflow® chart that, based on numeric input data, concatenates string data into natural language output text. You can view the output text in the Simulation Data Inspector and the MATLAB® workspace. For more information about string data, see Manage Textual Information by Using Strings.

Flow chart that produces string output based on the numerical values of latitude and longitude.

Chart Behavior

The model in this example uses Sine Wave blocks to provide the latitude and longitude of a point moving along a closed path. The chart examines these coordinates and assigns the strings q1 and q2 according to the information in this table.


Then, the statement

sout = strcat("Location in ",q1,q2," Quadrant");
concatenates these strings into an output string.

Create the Model

Add Junctions and Transitions

  1. To create a Simulink® model with an empty chart, at the MATLAB command prompt, enter


  2. In the empty chart, place a default transition. A junction appears. Click and drag from the edge of the junction to add other transitions and junctions.

  3. Double-click each transition. At the text prompt, enter the appropriate condition or action statement.

Define Chart Data

  1. To resolve the undefined data, in the Symbols pane, click the Resolve undefined symbols icon . The Stateflow Editor assigns an appropriate scope to each symbol in the chart.

    latitudeInput Data
    longitudeInput Data
    q1Local Data
    q2Local Data
    soutOutput Data

  2. To specify q1 as string data, click q1 and, in the Property Inspector, set Type to string.

  3. Repeat that specification for q2 and sout.

    Alternatively, to create string data with a maximum number of characters, specify each string as stringtype(n), where n is a suitable buffer size that avoids truncating the contents of the string. For instance, this table lists suitable buffer sizes for the string data in the chart.

    SymbolNumber of CharactersString Data Type

Add Sources and Sinks to the Model

  1. In the Simulink model, add two Sine Wave blocks and a Display block. Connect the blocks to the chart input and output ports.

    Simulink model that contains a Stateflow chart, two Sine Wave blocks, and a Display block.

  2. Set the Sine Wave block parameters as indicated in this table.


  3. Label the signals in the model as latitude, longitude, and sout.

  4. Right-click each signal and select Log Selected Signals.

View Simulation Results

  1. When you simulate the model, the Data Inspector button is highlighted to indicate that it has new simulation data. To open the Simulation Data Inspector, click the button.

  2. In the Simulation Data Inspector, select the check boxes for the latitude, longitude, and sout signals to display the signals on the same set of axes. The latitude and longitude signals appear as sinusoidal curves. The sout signal is a transition plot. The transition plot displays the value of the string inside a band and criss-crossed lines mark the changes in value.

    Simulation Data Inspector showing output from the simulation.

  3. To access the logged data in the MATLAB workspace, call the signal logging object logsout. Stateflow exports the string data sout as a MATLAB string scalar. For example, at the command prompt, enter:

    logsout = out.logsout;
    Tbl = table(logsout.get('latitude').Values.Data, ...
       logsout.get('longitude').Values.Data, ...
    Tbl.Properties.VariableNames = ...
    ans =
      4×3 table
        Latitude    Longitude              QuadrantInfo          
        ________    _________    ________________________________
         28.232       41.267     "Location in Northeast Quadrant"
         40.425      -29.425     "Location in Northwest Quadrant"
        -30.593      -39.548     "Location in Southwest Quadrant"
        -38.638       31.735     "Location in Southeast Quadrant"

See Also

| (Simulink) |

Related Topics