| Contents | Index |
When you simulate a chart, you can log values for local data and state activity into Simulink objects. After simulation, you can access these objects in the MATLAB workspace and use them to report and analyze the values.
When you log a state, its value is 1 when active and 0 when inactive.
Logging Stateflow data and state activity follows the same general guidelines as for logging signals in Simulink models.
Exporting Signal Data Using Signal Logging in the Simulink documentation.
The workflow for logging chart local data and state activity is similar to the workflow for logging signals in a model:
Enable signal logging for the chart and choose a logging format.
Configure states and local data for signal logging, which includes controlling how much output the simulation generates.
Simulate the chart.
Access the logged data.
The procedures that take you through the logging workflow use sf_semantics_hotel_checkin. The model simulates a hotel check-in process. To open the model, click sf_semantics_hotel_checkin or type sf_semantics_hotel_checkin at the MATLAB command prompt.
In this model, the chart Hotel controls activities that trigger transitions to different rooms in the hotel. The chart contains a hierarchy of nested states and uses two local variables:
| Local Variable | Description |
|---|---|
| move_bags | Indicates whether bags should move to another room or stay in the current room. |
| service | Accumulates the number of room service calls. |
Open the chart and select Simulation > Configuration Parameters.
Select Data Import/Export.
In the Signals pane, select the Signal logging check box to enable logging for the chart.
Signal logging is enabled by default for models and charts. To disable logging, clear the check box.
Optionally, specify a custom name for the signal logging object.
The default name is logsout. Using this object, you can access the logging data in a MATLAB workspace variable (see Signal Logging Object).
Specify a format for logged data from the Signal logging format menu.
Click OK.
Stateflow charts support the same formats for logged data as Simulink models:
| Format | Description |
|---|---|
| Dataset | Stores logged data as MATLAB timeseries objects in objects of the value class Simulink.SimulationData.Dataset. |
| ModelDataLogs | Stores logged data in objects of the handle class Simulink.ModelDataLogs. |
Advantages of Dataset Format. The Dataset format offers several advantages over the ModelDataLogs format for logging chart data and state activity:
Supports logging multiple changes to data values for a given time step.
Provides a getElement function for easy access to logged data in charts with deep hierarchies.
Provides consistent logging output when charts appear in referenced models.
Represents model hierarchy as a flat list for easy access by index to nested elements.
Supports logging of atomic subchart activity.
Specifying the Signal Logging Data Format in the Simulink documentation
Benefits of Using the Dataset Format for Signal Logging in the Simulink documentation
You can configure the same properties for logging states and local data in a chart as you can for logging signals in a model:
| Property | Description |
|---|---|
| Log signal data | Saves the signal's value to the MATLAB workspace during simulation. |
| Logging name | Name of the logged signal. Defaults to the original name of the state or local data. To rename the logged signal, select Custom and enter a new name. For guidance on when to use a different name for a logged signal, see Specifying a Logging Name in the Simulink documentation. |
| Limit data points to last | Limits the amount of data logged to the most recent samples. See Limit Data Points to Last in the Simulink documentation. |
| Decimation value | Limits the amount of data logged by skipping samples. For example, a decimation factor of 2 saves every other sample. See Decimation in the Simulink documentation. |
Logging and Accessibility Options in the Simulink documentation
There are several ways to configure states and local data for logging:
| Method | When to Use |
|---|---|
| Logging Individual States and Data | Configure states or local data for logging one at a time from inside the chart. |
| Logging Multiple Signals At Once | Configure multiple signals for logging from a list of all states and local data. |
| Logging Chart Signals Using the Command-Line API | Configure logging properties programmatically. |
Open the properties dialog box for the state or local data.
| For: | Do This: |
|---|---|
| States | Right-click the state and select Properties. |
| Local data | Right-click the state or transition that uses the local data and select Explore > (data)variable_name. |
In the properties dialog box, click the Logging tab.
Modify properties as needed, as described in Properties to Configure for Logging.
For example, from the Hotel chart of the sf_semantics_hotel_checkin model:
Open the properties dialog box for the service local data and select the Log signal data check box.
Open the properties dialog box for the Dining_area state, select the Log signal data check box, and change the logging name to Dining_Room.
In the model, right-click the Stateflow chart and select Log Chart Signals.
The Stateflow Signal Logging dialog box opens, showing all states and local data. These chart objects are the signals you can log.
Select the check box next to each signal you want to log.
The Log signal data check box is selected automatically for each signal you log. For example, in the Hotel chart of the sf_semantics_hotel_checkin model, log the Check_in.Checked_in.Executive_suite.Dining_area state and local variable service:

For each signal you select, modify the properties of what gets captured in the log.
For example, change the logging name of Check_in.Checked_in.Executive_suite.Dining_area to Dining_Room.
For a description of each property, see Properties to Configure for Logging.
Open the model that contains the chart.
For example, open the sf_semantics_hotel_checkin model, which has a chart called Hotel.
Get the states whose activity you want to log.
For example, get the Dining_area state in the Hotel chart:
rt = sfroot;
da_state = rt.find('-isa','Stateflow.State','Name','Dining_area');Get the local data you want to log.
For example, get the service local data in the Hotel chart:
svc_data = rt.find('-isa','Stateflow.Data','Name','service');Enable logging for states and data.
For example, enable logging for the Dining_area state and the service data:
da_state.LoggingInfo.DataLogging = 1; svc_data.LoggingInfo.DataLogging = 1;
Modify logging properties as needed.
For example, change the logged name of the Dining_area state. By default, the logged name is the hierarchical signal name, which is Check_in.Checked_in.Executive_suite.Dining_area. To assign the shorter, custom name of Dining_Room:
% Enable custom naming da_state.LoggingInfo.NameMode = 'Custom'; % Enter the custom name da_state.LoggingInfo.UserSpecifiedLogName = 'Dining_Room';
Logging Multiple Signals At Once
During simulation, Stateflow saves logged data in a signal logging object, which you can access in the MATLAB workspace. The type of signal logging object depends on the signal logging format that you choose.
| Format | Signal Logging Object |
|---|---|
| Dataset | Simulink.SimulationData.Dataset |
| ModelDataLogs | Simulink.ModelDataLogs |
The default name of the signal logging object is logsout.
Enabling Signal Logging and Choosing a Logging Format to learn how to change the name of the signal logging object
Simulink.SimulationData.Dataset reference page in the Simulink documentation
Simulink.ModelDataLogs reference page in the Simulink documentation
View the signal logging object in the MATLAB environment.
For example:
Start simulating the sf_semantics_hotel_checkin model using the Dataset signal logging format.
When the Front_desk state becomes active, check in to the hotel by toggling the first switch.
When the Bedroom state in the Executive_suite state becomes active, order room service multiple times, for example, by toggling the second switch 10 times.
Stop simulation.
Enter:
logsout
Result:
Simulink.SimulationData.Dataset
Package: Simulink.SimulationData
Characteristics:
Name: 'logsout'
Total Elements: 2
Elements:
1: 'Dining_Room'
2: 'service'The output indicates:
logsout is a Simulink object of type SimulationData.Dataset.
Two elements were logged.
Use the getElement method to access logged elements by index and by name.
For example:
To access logged activity for the Check_in.Checked_in.Executive_suite.Dining_area state:
| By: | Enter: |
|---|---|
| Index | logsout.getElement(1) |
| Name | logsout.getElement('Dining_Room') |
| Block path |
|
The result is a Stateflow.SimulationData.State object:
Stateflow.SimulationData.State
Package: Stateflow.SimulationData
Properties:
Name: 'Dining_Room'
BlockPath: [1x1 Simulink.SimulationData.BlockPath]
Values: [1x1 timeseries]
To access logged activity for the service local data:
| By: | Enter: |
|---|---|
| Index | logsout.getElement(2) |
| Name | logsout.getElement('service') |
| Block path |
|
The result is a Stateflow.SimulationData.Data object:
Stateflow.SimulationData.Data
Package: Stateflow.SimulationData
Properties:
Name: 'service'
BlockPath: [1x1 Simulink.SimulationData.BlockPath]
Values: [1x1 timeseries]The logged values for Stateflow.SimulationData.State and Stateflow.SimulationData.Data objects are stored in the Values property as Simulink objects of type Timeseries.
Access logged data and time through the Values property.
For example:
| For: | Enter: |
|---|---|
| Data | logsout.getElement(1).Values.Data; |
| Time | logsout.getElement(1).Values.Time; |
View the logged data.
See Viewing Logged Data.
View the signal logging object in the MATLAB environment.
For example:
Start simulating the sf_semantics_hotel_checkin model using the ModelDataLogs signal logging format.
When the Front_desk state becomes active, check in to the hotel by toggling the first switch.
When the Bedroom state in the Executive_suite state becomes active, order room service multiple times, for example, by toggling the second switch 10 times.
Stop simulation.
Enter:
logsout
Result:
Simulink.ModelDataLogs (sf_semantics_hotel_checkin): Name Elements Simulink Class Hotel 2 StateflowDataLogs
The output indicates:
logsout is a Simulink object of type Simulink.ModelDataLogs.
Two elements were logged, all in the Hotel chart.
List the logged states and local data.
For example:
logsout.Hotel
Result:
Simulink.StateflowDataLogs (Hotel): Name Elements Simulink Class service 1 Timeseries Dining_Room 1 Timeseries
The logged signals are stored as Simulink objects of type Timeseries.
Use dot notation to access logged values for data or state activity.
Because of the way Simulink stores logged signals in ModelDataLogs format, you must use dot notation to access logged data for Stateflow objects below the chart level in the model hierarchy.
For example, to access logged values for service data, enter:
logsout.Hotel.service
Result:
Name: 'service'
BlockPath: 'sf_semantics_hotel_checkin/Hotel/service'
PortIndex: 1
SignalName: 'service'
ParentName: 'service'
TimeInfo: [1x1 Simulink.TimeInfo]
Time: [560498x1 double]
Data: [560498x1 double] |
A structure stores all logging information. The logged data values reside in the Data field and the logged times reside in the Time field. The size of your Time and Data vectors might differ from those shown here, depending on how you interact with the switches in the model.
View the logged data.
See Viewing Logged Data.
Use one of the following approaches to view logged data.
| Approach | Action |
|---|---|
| View logged data in a figure window | Use the plot function
for the Timeseries object. logsout.Hotel.service.plot |
| View logged data in a spreadsheet | Pass a numeric, cell, or logical array of logged values
to the xlswrite function.
|
Chart instances inherit logging properties from the library chart to which they are linked. You can override logging properties in the instance, but only for signals you select in the library. You cannot select additional signals to log from the instance.
To override properties of logged signals in chart instances, use one of the following approaches.
| Approach | How To Use |
|---|---|
| Simulink Signal Logging Selector dialog box | See Overriding Logging Properties with the Logging Selector |
| Command-line interface | See Overriding Logging Properties with the Command-Line API |
This example uses sf_atomic_sensor_pair. This model simulates a redundant sensor pair as atomic subcharts Sensor1 and Sensor2 in the chart RedundantSensors. Each atomic subchart contains instances of the states Fail, FailOnce, and OK from the library chart sf_atomic_sensor_lib.
Overriding Logging Properties with the Logging Selector.
Open the example library by clicking sf_atomic_sensor_lib or typing sf_atomic_sensor_lib at the MATLAB command prompt.
In the library, right click SingleSensor and select Log Chart Signals.
In Stateflow Signal Logging dialog box, set the following logging properties, then click OK.
| For Signal: | What to Specify: |
|---|---|
| Fail |
|
| FailOnce |
|
| OK |
|
Open the model that contains instances of the library chart by clicking sf_atomic_sensor_pair or typing sf_atomic_sensor_pair at the MATLAB command prompt.
Open the RedundantSensors chart and select Simulation > Configuration Parameters.
In the Data Import/Export pane, click Configure Signals to Log to open the Simulink Signal Logging Selector.
In the Model Hierarchy pane, expand RedundantSensors, and click Sensor1 and Sensor2.
Each instance inherits logging properties from the library chart. For example:

Now, override some logging properties for Sensor1:
In the Model Hierarchy pane, select Sensor1.
Change Logging Mode to Override signals.
The selector clears all DataLogging check boxes for the model.
Enable logging only for the Fail and FailOnce states in Sensor1:
Select DataLogging for these two signals. Leave DataLogging cleared for the OK signal.
Append the string Sensor1 to the logging names for Fail and FailOnce:
Double-click the logging names for signals Fail and FailOnce, and rename them LogFailSensor1 and LogFailOnceSensor1, respectively.
The settings should look like this:

Overriding Logging Properties with the Command-Line API.
Open the example library by clicking sf_atomic_sensor_lib or typing sf_atomic_sensor_lib at the MATLAB command prompt.
Log the signals Fail, FailOnce, and OK in the SingleSensor chart using these commands:
% Get states in the SingleSensor chart rt=sfroot; states = rt.find('-isa', 'Stateflow.State'); % Enable logging for each state for i = 1: length(states) states(i).LoggingInfo.DataLogging = 1; end |
Open the model that contains instances of the library chart by clicking sf_atomic_sensor_pair or typing sf_atomic_sensor_pair at the MATLAB command prompt.
Create a ModelLoggingInfo object for the model.
This object contains a vector Signals that stores all logged signals.
mi = Simulink.SimulationData.ModelLoggingInfo. ... createFromModel('sf_atomic_sensor_pair') |
The result is:
mi =
Simulink.SimulationData.ModelLoggingInfo
Package: Simulink.SimulationData
Properties:
Model: 'sf_atomic_sensor_pair'
LoggingMode: 'OverrideSignals'
LogAsSpecifiedByModels: {}
Signals: [1x6 Simulink.SimulationData.SignalLoggingInfo] |
The Signals vector contains the signals marked for logging in the library chart:
Library instances of Fail, FailOnce, and OK states in atomic subchart Sensor1
Library instances of Fail, FailOnce, and OK states in atomic subchart Sensor2
Make sure that LoggingMode equals 'OverrideSignals'.
Create a block path to each logged signal whose properties you want to override.
To access signals inside Stateflow charts, use Simulink.SimulationData.BlockPath(paths, subpath), where subpath represents a signal inside the chart.
To create block paths for the signals Fail, FailOnce, and OK in the atomic subchart Sensor1 in the RedundantSensors chart:
failPath = Simulink.SimulationData. ... BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','Fail') failOncePath = Simulink.SimulationData. ... BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','FailOnce') OKPath = Simulink.SimulationData. ... BlockPath('sf_atomic_sensor_pair/RedundantSensors/Sensor1','OK') |
Get the index of each logged signal in the Simulink.SimulationData.BlockPath object.
To get the index for the signals Fail, FailOnce, and OK:
failidx = mi.findSignal(failPath); failOnceidx = mi.findSignal(failOncePath); OKidx = mi.findSignal(OKPath); |
Override some logging properties for the signals in Sensor1:
Disable logging for signal OK:
mi.Signals(OKidx).LoggingInfo.DataLogging = 0;
Append the string Sensor1 to the logging names for Fail and FailOnce:
% Enable custom naming mi.Signals(failidx).LoggingInfo.NameMode = 1; mi.Signals(failOnceidx).LoggingInfo.NameMode = 1; % Enter the custom name mi.Signals(failidx).LoggingInfo.LoggingName = 'LogFailSensor1'; mi.Signals(failOnceidx).LoggingInfo.LoggingName = 'LogFailOnceSensor1'; |
Apply the changes:
set_param(bdroot, 'DataLoggingOverride', mi);Stateflow logs each update to a multidimensional signal as a single change. For example, an update to a 2-by-2 matrix A during simulation is logged as a single change, not as four changes (one for each element):
| Update | Is Logged As |
|---|---|
| A = 1; | A single change, even though the statement implies all A[i] = 1 |
| A[1][1] = 1; A[1][2] = 1; | Two different changes |
You cannot log bus data.
![]() | Monitoring Test Points in Stateflow Charts | Exploring and Modifying Charts | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |