Main Content

Log Data to the Workspace and a File Using the Record Block

When you log data using the Record block, you can log data to the workspace, to a file, or to both the workspace and a file. You can modify the recording settings for the Record block between simulations as needed. This example shows how to access data in the workspace and in a file after simulation, how to configure the Record block properties, and how to modify the data visualization in the Record block. The model for the example is simple: a Record block logs data from a Sine Wave block, a Chirp Signal block, and a Square Wave Generator block.

open_system('RecordWorkspaceAndFile');

To log data to the workspace and to the MLDATX file, simulate the model.

out = sim('RecordWorkspaceAndFile');

Access Data Logged to the Workspace

By default, model simulations return all logged data in a single Simulink.SimulationOutput object using the variable out. Type out in the Command Window to see the contents of the Simulink.SimulationOutput object.

out
out = 

  Simulink.SimulationOutput:
              recordout: [1x1 Simulink.SimulationData.Dataset] 
                   tout: [51x1 double] 

     SimulationMetadata: [1x1 Simulink.SimulationMetadata] 
           ErrorMessage: [0x0 char] 

The logged workspace data for the Record block is inside the out workspace variable, stored in the Simulink.SimulationData.Dataset object recordout. Type out.recordout in the Command Window to view the contents of the recordout Dataset object.

out.recordout
ans = 

Simulink.SimulationData.Dataset 'Run 1: RecordWorkspaceAndFile' with 3 elements

                         Name          PropagatedName  BlockPath                     
                         ____________  ______________  _____________________________ 
    1  [1x1 Signal]      Chirp Signal  Chirp Signal    RecordWorkspaceAndFile/Record
    2  [1x1 Signal]      Sine Wave     Sine Wave       RecordWorkspaceAndFile/Record
    3  [1x1 Signal]      Square Wave   Square Wave     RecordWorkspaceAndFile/Record

  - Use braces { } to access, modify, or add elements using index.

The data for each signal connected to the Record block is stored in a Simulink.SimulationData.Signal object. The data and time values for the signal are stored as a timeseries object in the Values property of the Signal object. Access the data for the Square Wave signal.

recorddata = out.recordout;
sqsignal = get(recorddata,3);
sqts = sqsignal.Values;
sqWaveData = sqts.Data;

You can also access the data by combining all the steps into a single line of code.

sqWaveData = get(out.recordout,3).Values.Data;

Access Data Logged to a File

This example logs data to a file in the working directory. After simulating the model, you see the recording.mldatx file in the Current Folder pane of the MATLAB® window. You can view the data in the MLDATX file using the Simulation Data Inspector. Double-click the file or open the file using the open function.

open('recording.mldatx');

In the Simulation Data Inspector, you can inspect the signal data using cursors or the replay controls, and you can build custom visualizations. You can use the Simulation Data Inspector programmatic interface to access the data from the MLDATX file in the workspace or a script, and you can export the data to the workspace, a MAT file, or an Excel® file.

You can also configure the Record block to log data to a MAT file or an Excel file. To access and analyze data logged to a MAT file, you can load the data into the base workspace, or you can import the data into the Simulation Data Inspector. You can access and analyze data logged to an Excel file in Excel, by reading the data into the base workspace, or by importing the data into the Simulation Data Inspector.

Configure Recording Settings

View and modify the recording parameters for the Record block using the Property Inspector. To display the Property Inspector, on the Modeling tab, under Design, select Property Inspector. To view the parameters for the Record block in the model, select the Record block. The Record block in this example is configured to log data to the workspace using the variable recordout and to log data to the file recording.mldatx. Using the recording settings, you can disable logging to the workspace or logging to file, and you can change the File Type parameter to log data to a MAT file or an Excel file and specify the location for the logged file.

View Data Using the Record Block

The Record block also visualizes the connected signals. To view the connected signal data, double-click the Record block. By default, the Record block displays each connected signal as a sparkline.

You can change the layout and display data on other types of visualizations, including an XY plot, a map, and time plots. For example, change the visualization to a time plot. On the Simulation tab, under Prepare, select Sparklines and then Time Plot. When you change to a different visualization, plotted signals are cleared from the subplot. Next, change the layout to include three subplots. From the Layouts menu, select the Column Right option.

To plot signals in the time plots, expand the signals table by clicking Show Signals in the upper-left. Check the box next to the signal you want to plot on the selected subplot, outlined in blue. For example, plot the Sine Wave signal in the first subplot. Then, select another subplot and plot the Chirp signal. Select the final subplot and plot the Square Wave signal.

When you save the model, the updated visualization is saved in the Record block. When you simulate the model, the Record block updates the plots with data from the current simulation.

See Also

Blocks

Tools

Objects

Model Settings

Related Topics