Import Data from a CSV File into the Simulation Data Inspector
To import data into the Simulation Data Inspector from a CSV file, format the data in the CSV file. Then, you can import the data using the Simulation Data Inspector UI or the Simulink.sdi.createRun
function.
Tip
When you want to import data from a CSV file where the data is formatted differently from the specification in this topic, you can write your own file reader for the Simulation Data Inspector using the io.reader
class.
Basic File Format
In the simplest format, the first row in the CSV file is a header that lists the names of the signals in the file. The first column is time. The name for the time column must be time
, and the time values must increase monotonically. The rows below the signal names list the signal values that correspond to each time step.
The import operation does not support time data that includes Inf
or NaN
values or signal data that includes Inf
values. Empty or NaN
signal values render as missing data. All built-in data types are supported.
Multiple Time Vectors
When your data includes signals with different time vectors, the file can include more than one time vector. Every time column must be named time
. Time columns specify the sample times for signals to the right, up to the next time vector. For example, the first time column defines the time for signal1
and signal2
, and the second time column defines the time steps for signal3
.
Signal columns must have the same number of data points as the associated time vector.
Signal Metadata
You can specify signal metadata in the CSV file to indicate the signal data type, units, interpolation method, block path, and port index. List metadata for each signal in rows between the signal name and the signal data. Label metadata according to this table.
Signal Property | Label | Value |
---|---|---|
Data type | Type: | Built-in data type. |
Units | Unit: | Supported unit. For example, For a list of supported units, enter |
Interpolation method | Interp: | linear , zoh for zero order hold, or none . |
Block Path | BlockPath: | Path to the block that generated the signal. |
Port Index | PortIndex: | Integer. |
You can also import a signal with a data type defined by an enumeration class. Instead of using the Type:
label, use the Enum:
label and specify the value as the name of the enumeration class. The definition for the enumeration class must be saved on the MATLAB path.
When an imported file does not specify signal metadata, the Simulation Data Inspector assumes double data type and linear interpolation. You can specify the interpolation method as linear, zoh (zero-order hold), or none. If you do not specify units for the signals in your file, you can assign units to the signals in the Simulation Data Inspector after you import the file.
You can specify any combination of metadata for each signal. Leave a blank cell for signals with less specified metadata.
Import Data from a CSV File
You can import data from a CSV file using the Simulation Data Inspector UI or using the Simulink.sdi.createRun
function.
To import data using the UI, open the Simulation Data Inspector using the Simulink.sdi.view
function or the Data Inspector button in the Simulink™ toolstrip. Then, click Import .
In the Import dialog, select the option to import data from a file and navigate in the file system to select the file. After you select the file, data available for import shows in the table. You can choose which signals to import and whether to import them to a new or existing run. This example imports all available signals to a new run. To select all or none of the signals, select or clear the check box next to NAME. After selecting the options, click the Import button.
When you import data into a new run using the UI, the new run name includes the run number followed by Imported_Data
.
When you import data programmatically, you can specify the name of the imported run.
csvRunID = Simulink.sdi.createRun('CSV File Run','file','csvExampleData.csv');