Main Content

Simulink.sdi.addToRun

Import data into existing run in Simulation Data Inspector using run ID

Description

Import Data from Workspace

sigIDs = Simulink.sdi.addToRun(runID,'vars',var,var2,...,varn) imports data from one or more variables into the Simulation Data Inspector by adding one or more signals to the run that corresponds to the specified run ID.

To import data into a new run, use the Simulink.sdi.Run.create function or the Simulink.sdi.createRun function.

example

sigIDs = Simulink.sdi.addToRun(runID,'namevalue',sourceNames,sigValues) imports data from one or more variables into the Simulation Data Inspector by adding one or more signals to the run that corresponds to the specified run ID. The sourceNames argument specifies values to use for the data source in the metadata for the signals added to the run.

Import Data from File

sigIDs = Simulink.sdi.addToRun(runID,'file',filename) imports data from a file into the Simulation Data Inspector by adding one or more signals to the run that corresponds to the specified run ID. You can use a built-in file reader to import data from a MAT file, CSV file, Microsoft® Excel® file, or MDF file.

When you need to import data from a file that the built-in readers do not support, you can write your own reader using the io.reader class.

sigIDs = Simulink.sdi.addToRun(runID,'file',filename,Name=Value) imports data from a file into the Simulation Data Inspector by adding one or more signals to the run that corresponds to the specified run ID according to options specified using one or more name-value arguments. For example, sheets=["sheet1" "sheet2"] specifies the sheets from which to import data when importing data from an Excel file.

Examples

collapse all

This example shows how to use Simulink.sdi.addToRun to add workspace data to a run in the Simulation Data Inspector.

Generate Workspace Data

Generate workspace data to add to a simulation run in place of measured data, input data, or any other data that you want to associate with the simulation.

time = linspace(0, 60, 201);
cos_vals = 2*cos(2*pi/6.8*time);
cos_ts = timeseries(cos_vals, time);
cos_ts.Name = 'cosine';

Simulate Model

Simulate the slexAircraftExample model to create a run containing the simulation outputs.

load_system('slexAircraftExample');
sim('slexAircraftExample','SaveFormat','Dataset');

Add Workspace Data to Simulation Run

Add the workspace data to the run. Then, view the results in the Simulation Data Inspector.

% Get run ID
count = Simulink.sdi.getRunCount;
runID = Simulink.sdi.getRunIDByIndex(count);

% Add data to run
sigIDs = Simulink.sdi.addToRun(runID,'vars',cos_ts);

Simulink.sdi.view

Input Arguments

collapse all

Run ID for run to which you want to add imported data, specified as a scalar.

The Simulation Data Inspector assigns a unique ID to each run when the run is created. You can access run IDs for runs in the Simulation Data Inspector using the Simulink.sdi.getAllRunIDs and Simulink.sdi.getRunIDByIndex functions.

Data to import, specified as a variable. The Simulation Data Inspector supports time-based data in which sample values are associated with sample times. The Simulation Data Inspector supports all loading and logging data formats, including timeseries and Simulink.SimulationData.Dataset.

Example: myData

Source names for imported data, specified as a cell array of character vectors. The source name is used to set the RootSource, TimeSource, and DataSource properties of the Simulink.sdi.Signal objects created from the data specified by the sigValues input.

Provide a sourceNames input when you specify 'namevalue' for the second argument.

Example: {"sig1","sig2"}

Data to import, specified as a cell array of variables.

Provide a sigValues input when you specify 'namevalue' for the second argument.

Example: {var1,var2}

Name of file with data to import, specified as a character vector. Provide a filename input when you specify "file" for the second argument.

You can create a run from these types of files using file readers built into the Simulation Data Inspector:

  • MAT file.

  • CSV file.

  • Microsoft Excel file that contains data formatted according to Microsoft Excel Import, Export, and Logging Format.

  • MDF file with one of these extensions:

    • .mdf

    • .mf4

    • .mf3

    • .data

    • .dat

  • ULG file. Flight log data import requires a UAV Toolbox license.

  • ROS Bag file version 1.1 or 2.0. Bag file import requires a ROS Toolbox license.

When you need to import data from a file that the built-in readers do not support, you can write your own reader using the io.reader class. You can also write a custom reader to use instead of the built-in reader for any file extension. For an example, see Import Data Using Custom File Reader.

Example: 'simulation.mat'

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: sheets=["sheet1" "sheet2"]

File reader to use to import data, specified as a string or character vector.

The Simulation Data Inspector prioritizes using a registered custom reader when one is available for the file. When you do not specify a reader, the Simulation Data Inspector uses the first custom reader registered for the file. If no custom readers are registered, the data is imported using the built-in reader.

Specify the reader input when:

  • You want to use the built-in reader to import data for a file that is also supported by a custom reader.

  • Multiple registered custom readers support the file.

To determine which readers are available to import your file, use the io.reader.getSupportedReadersForFile function.

Example: "MyExcelReader"

Example: "built-in"

Sheets in Excel file from which to import data, specified as a string array or a cell array of character vectors. By default, the Simulation Data Inspector imports data from all sheets. Use the sheets name-value argument when you do not want to import data from all sheets in the Excel file.

When the data in the file does not include simulation numbers and source information, the data on each sheet is imported into a separate run. For more information about formatting data to import from an Excel file, see Microsoft Excel Import, Export, and Logging Format.

Example: ["sheet1" "sheet2"]

Model with definitions of user-defined data types, specified as a string or character vector.

When you load data from an Excel file that defines signal data types using user-defined data types, such as enumerations, buses, or aliases, the Simulation Data Inspector needs access to the type definition to import the data. You can provide access to the type definitions by:

  • Loading the associated object into the MATLAB® workspace.

  • Specifying the model name-value argument to use type definitions saved in the model workspace or a data dictionary.

For more information on formatting data to import from an Excel file, see Microsoft Excel Import, Export, and Logging Format.

Example: "myModel.slx"

Output Arguments

collapse all

Signal IDs for signals created from imported data, returned as a scalar or a vector.

Version History

Introduced in R2011b