Exporting (logging) simulation data provides a baseline for analyzing and debugging a model. Use standard or custom MATLAB® functions to generate simulated system input signals and to graph, analyze, or otherwise postprocess the system outputs.
Simulation data can include any combination of signal, time, output, state, and data store logging data.
Exporting simulation data involves saving signal values to the MATLAB workspace or to a MAT-file during simulation for later retrieval and postprocessing. Exporting data is also known as “data logging” or “saving simulation data.”
You can have data logged in several formats:
Array
Structure
Structure with time
MATLAB timeseries
ModelDataLogs
Note
The ModelDataLogs
format is supported for backward
compatibility. Starting in R2016a, you cannot log data in the
ModelDataLogs
format. In R2016a or later, when you open a
model from an earlier release that had used ModelDataLogs
format,
the model logs data in Dataset
format.
Consider converting data logged in other formats to Dataset
format to
simplify post-processing. For more information, see Dataset Conversion for Logged Data.
You can also use exported data as the input for simulating a model.
Exporting simulation data often involves exporting signal data. You can use various approaches for exporting signal data.
Export Approach | Usage | Documentation |
---|---|---|
Connect a Scope block to a signal. | If you use a Scope block for viewing results during simulation, consider also using the Scope block to export data. Save output at a sample rate other than the base sample rate. Scopes store data and can be memory intensive. | |
Connect a signal to a To File block. | Consider using a To File block for exporting large amounts of data. Save output at a sample rate other than the base sample rate. Use the MAT-file only after the simulation has completed. | |
Connect a signal to a To Workspace block. | Document in the diagram the workspace variables used to store signal data. Save output at a sample rate other than the base sample rate. | |
Connect a signal to a root-level Outport block. | Consider using this approach for logging data in a top-level model, if the model already includes an Outport block. | |
Set the signal logging properties for a signal. | Use signal logging to avoid adding blocks, such as Scope, To File, and To Workspace blocks, to your model. Log signals based on individual signal rates. Data is available when simulation is paused or completed. Use signal logging to log array of buses signals. | |
Configure Simulink® to export time, state, and output data. | To capture complete information about the simulation as a whole, consider exporting this data. Use the Output parameter to save root Outport block data during simulation. Outputs and states are logged at the base sample rate of the model. | |
Log a data store. | Log a data store to share data throughout a model hierarchy, capturing the order of all data store writes. | Log Data Stores |
Use the | Use Select the Return as
single object parameter when simulating the model using the
| sim |
To export the states and root-level output ports of a model to the MATLAB base workspace during simulation of the model, use one of these interfaces:
Configuration Settings > Data Import/Export pane (for details, see Model Configuration Parameters: Data Import/Export)
sim
command
In both approaches, specify:
The kinds of simulation data that you want to export:
Signal logging
Time
Output
State or final state
Data store
Each kind of simulation data export has an associated default variable. You can specify your own variables for the exported data.
The characteristics of the logged data, including:
To inspect exported simulation data interactively, consider using the Simulation Data Inspector.
The Simulation Data Inspector has some limitations on the kinds of logged data that it displays. See View Data in the Simulation Data Inspector.
When exporting simulation data in a simulation mode other than rapid accelerator, Simulink optimizes memory usage in the following situations.
When time steps happen at regular intervals, Simulink uses compressed time representation. Simulink stores the value for the first timestamp, the length of the interval (time step), and the total number of timestamps.
When multiple signals use identical timestamp sequences, the signals share a single stored timestamp sequence. Sharing a single stored timestamp can reduce memory use for logged data by as much as a factor of two. The difference in memory performance can be a critical performance factor, particularly when logging bus signals that have thousands of bus elements.
You can encounter memory issues when you log many signals in a long simulation that has many time steps. Logging to persistent storage can address this kind of memory issue.
To log to persistent storage, in the Configuration Parameters > Data Import/Export pane, select Log Dataset data to file option. Specify the kinds of logging (for example, signal logging and states logging).
For logging output and states data, set the Format parameter
to Dataset
.
If you select the Final states parameter, clear the Save final operating point parameter.
Using a Simulink.SimulationData.DatasetRef
object to access signal
logging and states logging data loads data into the model workspace incrementally.
Accessing data for other kinds of logging loads all the data at once.
For details, see Log Data to Persistent Storage.