Load Data Using the From File Block
This example shows how to load simulation input data from a MAT file using the From File block, including how to create and format the input data. You can programmatically create the data you load, load data logged from another simulation, or load real-world data collected from sensors or in a lab. You can use the code from each format section as a template to understand how to format your own simulation input data.
Examine the Model
This example uses a simple model. Two From File blocks load data from two different MAT files to create signals that are connected to two Outport blocks. One From File block loads data for a scalar signal, and only the File name parameter for that block is changed from the default value. The other From File block is configured to load data for a bus. Two Dashboard Scope blocks display the output signals created by each From File block. The model also contains three Callback Button blocks you can use to generate the files with the input data using each format supported by the From File block.
The model uses the PreLoadFcn
callback to:
Create and save
timeseries
data in the fileinputData.mat
.Create and save input data for the bus in the file
busData.mat
.Create the
Simulink.Bus
object that defines the output data type for the From File block that loads data for a bus.
Use the Callback Button blocks to create data in the desired format prior to simulating the model. To create the data, click to select the Callback Button block and click again to run the code. You can view the code for each Callback Button block in the block dialog or Property Inspector.
Create Time and Signal Data
The From File block supports loading data stored in timeseries
objects and data stored in an array. Data you load using the From File block must contain a time value to correspond to each sample value. This example creates and loads ten seconds of data that represents a sine wave.
First, create a time vector. When you load data using the From File block, the data type for the time values must be double
, and the time values must increase monotonically.
sampleTime = 0.01; numSteps = 1001; time = sampleTime*(0:numSteps-1);
Use the expression in this example to create an evenly spaced time vector for an input signal, especially when modeling discrete input signals. MATLAB® supports several other methods for creating an evenly spaced time vector, but other methods can introduce double-precision rounding errors in the time data, which can lead to unexpected simulation results.
Now, create the signal data using the sin
function. The sample values you load using the From File block must not contain NaN
, Inf
, or -Inf
values.
data = sin(2*pi/3*time);
Load timeseries
Data
Simulink® loading and logging both commonly use timeseries
objects to pass time series data into and out of simulations. The From File block supports loading a variety of input data when you save the data in the MAT file as a timeseries object. The signal values you load can be:
A built-in numeric data type other than
half
,int64
, anduint64
; a fixed-point data type with up to a 32-bit word length; or an enumerated data type.Real or complex.
Scalar, vector, or multidimensional.
When you load data in a timeseries
object using the From File block, you must save the timeseries
data in a Version 7.3 MAT file. The To File block saves data to a Version 7.3 MAT file. When you load data from a file created using a To File block, you do not need to convert the version of the file.
The code for the Callback Button block labeled Create file with timeseries data to load
:
Creates the time and signal data as row vectors.
Transposes the row vectors to column vectors, as required to create a
timeseries
object with scalar data values.Creates a
timeseries
object to contain the data.Saves the
timeseries
object in a Version 7.3 MAT file.
sampleTime = 0.01; numSteps = 1001; time = sampleTime*[0:(numSteps-1)]; data = sin(2*pi/3*time); time = time'; data = data'; inputData = timeseries(data,time); save("inputData.mat","inputData","-v7.3");
To load the timeseries
data, you can run the code to create the inputData.mat
file using the MATLAB Command Window or select then click the Create file with timeseries data to load
Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.
Load Array Data
You can use the From File block to load scalar or vector signal data formatted as an array, where the first row contains time data and subsequent rows contain the sample values. When you load array data using the From File block, the sample values must be real and double
. The From File block can load array data from any MAT file versions.
The code for the Callback Button block labeled Create file with array data to load
creates the time and signal data, concatenates the row vectors into an array, and saves the array to a Version 7 MAT file.
sampleTime = 0.01; numSteps = 1001; time = sampleTime*[0:(numSteps-1)]; data = sin(2*pi/3*time); inputData = [time;data]; save("inputData.mat","inputData")
To load the array data, you can run the code to create the inputData.mat
file using the MATLAB Command Window or select then click the Create file with array data to load
Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.
Load Bus Data
The From File block supports loading a structure that contains timeseries
objects as input data for a bus. Each timeseries
object in the structure can contain any kind of data supported when loading data from a single timeseries
object.
When you load bus data using the From File block, you must specify the Output data type parameter as the Simulink.Bus
object that defines the bus. The hierarchy and field names in the structure that contains the bus data must match the hierarchy and element names of the Bus
object that defines the output data type.
The From File block that loads the bus data has its Output data type setting configured as Bus: SinusoidBus
. The PreLoadFcn
callback for the model and the code for the Create file with bus data to load
Callback Button block both define the SinusoidBus
object.
A Bus
object defines the bus hierarchy as well as properties of the elements in the bus, such as name and data type. The Bus
object in this example defines the bus hierarchy, the names for the signals contained in the bus, and the data type for a nested bus. The bus, SinusoidBus
, contains one signal, Cosine
, and a nested bus called SineBus
, which contains two signals, Sine
and BigSine
.
elems(1) = Simulink.BusElement; elems(1).Name = 'Sine'; elems(2) = Simulink.BusElement; elems(2).Name = 'BigSine'; SineBus = Simulink.Bus; SineBus.Elements = elems; clear elems; elems(1) = Simulink.BusElement; elems(1).Name = 'SineBus'; elems(1).DataType = 'Bus: SineBus'; elems(2) = Simulink.BusElement; elems(2).Name = 'Cosine'; SinusoidBus = Simulink.Bus; SinusoidBus.Elements = elems;
For more information about defining buses using Simulink.Bus
objects, see Specify Bus Properties with Bus Objects.
The Create bus data to load
Callback Button block creates a structure of timeseries
objects with a hierarchy and field names that match the hierarchy and element names of the SinusoidBus
object and saves the structure to a Version 7.3 MAT file.
sampleTime = 0.01; numSteps = 1001; time = sampleTime*[0:(numSteps-1)]; time = time'; data = sin(2*pi/3*time); cosdata = cos(2*pi/3*time); ampdata = 2*data; busData.Cosine = timeseries(cosdata,time); busData.SineBus.Sine = timeseries(data,time); busData.SineBus.BigSine = timeseries(ampdata,time); save("busData.mat","busData","-v7.3");
To load the bus data, you can run the code to create the busData.mat
file using the MATLAB Command Window or select then click the Create bus data to load
Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.
When you load input data for a bus using the From File block, you do not need to provide data for every element in the bus. To partially specify input data for a bus, you can omit the corresponding field in the structure or specify its value as []
. When you partially specify data for a bus that includes a nested bus, you can specify the entire nested bus as []
and the From File block provides ground values for all elements of the nested bus.