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 singleSimulink.SimulationOutputobject using the variableout。Typeoutin the Command Window to see the contents of theSimulink.SimulationOutputobject.

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 theoutworkspace variable, stored in theSimulink.SimulationData.Datasetobjectrecordout。Typeout.recordoutin the Command Window to view the contents of therecordoutDatasetobject.

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 aSimulink.SimulationData.Signalobject. The data and time values for the signal are stored as atimeseriesobject in theValuesproperty of theSignalobject. Access the data for theSquare Wavesignal.

recorddata = out.recordout;sqsignal = (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 therecording.mldatxfile 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 theopenfunction.

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 theModelingtab, underDesign, selectProperty 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 variablerecordoutand to log data to the filerecording.mldatx。Using the recording settings, you can disable logging to the workspace or logging to file, and you can change theFile Typeparameter 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 theSimulationtab, underPrepare, selectSparklinesand thenTime Plot。当你改变到一个不同的可视化、plotted signals are cleared from the subplot. Next, change the layout to include three subplots. From theLayoutsmenu, select theColumn Rightoption.

To plot signals in the time plots, expand the signals table by clickingShow Signalsin the upper-left. Check the box next to the signal you want to plot on the selected subplot, outlined in blue. For example, plot theSine Wavesignal in the first subplot. Then, select another subplot and plot theChirpsignal. Select the final subplot and plot theSquare Wavesignal.

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