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 in the MATLAB® workspace. For more information on string data, see Manage Textual Information by Using Strings.

Chart Behavior

During simulation, Sine Wave blocks provide the position of a point moving along a closed path in terms of latitude and longitude coordinates. The chart examines these coordinates and assigns the strings q1 and q2 according to the information in this table.

LatitudeLongitudeq1q2
positivepositive"North""east"
positivenegative"North""west"
negativepositive"South""east"
negativenegative"South""west"

Then, the statement

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

Build the Model

Add Junctions and Transitions

  1. To create a Simulink® model with an empty chart that uses the C action language, at the MATLAB command prompt, enter

    sfnew -c

  2. In the empty chart, place a default transition. A junction appears. Point 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.

    SymbolScope
    latitudeInput Data
    longitudeInput Data
    q1Local Data
    q2Local Data
    soutOutput Data

  2. To specify q1 as string data, in the Type field of the Property Inspector, select string. Repeat that specification for q2 and sout.

    Alternatively, to create string data with a maximum number of characters, specify each string as stringtype(n) using a suitable buffer size n to avoid truncation of its contents. For instance, this table lists suitable buffer sizes for the string data in the chart.

    SymbolNumber of CharactersString Data Type
    q15stringtype(5)
    q25stringtype(5)
    sout30stringtype(30)

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.

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

    BlockAmplitudeBiasFrequencyPhase
    Latitude50010
    Longitude5001pi/2

  3. Label the signals in the model as latitude, longitude, and sout. Right-click each signal and select Log Selected Signals.

View Simulation Results

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

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

  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:

    A = table(logsout.get('latitude').Values.Data, ...
       logsout.get('longitude').Values.Data, ...
       logsout.get('sout').Values.Data);
    A.Properties.VariableNames = {'Latitude','Longitude','QuadrantInfo'};
    A([4:8:30],:)
    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

| |

Related Topics