Main Content

Load Data Using the From Workspace Block

This example shows how to create and format data to load from the base workspace using the From Workspace block. To load data using the From Workspace block, you must create or save the data using a format the From Workspace block supports in the base, model, or mask workspace. You could programmatically create the data you load, load data logged in 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 Workspace blocks load data from the base workspace to create signals that are connected to two Outport blocks. One From Workspace block uses default values for all block settings, and the other is configured to load data for a bus. Two Dashboard Scope blocks display the output signals created by each From Workspace block. The model also contains several Callback Button blocks you can use to generate the data in the base workspace using different formats supported by the From Workspace block.

The model uses the PreLoadFcn callback to:

  • Create timeseries data stored in the variable simin.

  • Create a structure of timeseries objects stored in the variable busin.

  • Create the Simulink.Bus object that defines the output data type for the From Workspace block that loads data for a bus.

Use one of 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

Most data formats supported by the From Workspace block fundamentally consist of time values paired with signal values. You can use the Structure without time format to load only input data values with no corresponding time values, which is sometimes required for discrete simulations. (For more information, see Control How Models Load Input Data.) This example creates and loads ten seconds of time and signal data that represents a sine wave.

First, create a time vector. When you load data that includes time values using the From Workspace 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);
time = time';

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.

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 code for the Callback Button block labeled Create timeseries data to load creates the time and signal data, uses it to create a timeseries object, and assigns the timeseries object to the loading variable simin.

sampleTime = 0.01;
numSteps = 1001;
time = sampleTime*(0:numSteps-1);
time = time';
data = sin(2*pi/3*time);
simin = timeseries(data,time);

To load the timeseries data, you can run the code to create the simin loading variable in the MATLAB Command Window or select then click the Create timeseries data to load Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.

Load timetable Data

You can also store time series data in a MATLAB timetable. As the name suggests, the data is formatted as a table. When you load data using a timetable, the timetable must contain data for a single signal, in only one column.

The code for the Callback Button block labeled Create timetable data to load creates the time and signal data, formats the data as a timetable, and assigns the timetable to the loading variable simin. To load a timetable using the From Workspace block, the input times must be a duration vector. This example creates the duration vector, secs, using the seconds function.

sampleTime = 0.01;
numSteps = 1001;
time = sampleTime*(0:numSteps-1);
time = time';
data = sin(2*pi/3*time);
secs = seconds(time);
simin = timetable(secs,data);

To load the timetable data, you can run the code to create the simin loading variable in the MATLAB Command Window or select then click the Create timetable data to load Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.

Load Structure Data

The From Workspace block supports loading data for a signal using a structure that matches the Structure or Structure with time logging formats. This example shows how to create data that matches the Structure with time format. The Structure format is the same as the Structure with time format, except the Structure format does not have a time field.

The structure has two top-level fields, time and signals. When you log data using the Structure with time format, the signals field may be an array of structures that each contain data for a different signal. When you load data using the Structure with time format, the signals field can only contain one structure with data for a single signal. The structure for the signals field contains a values field and must contain a dimensions field when the signal is not scalar.

The code for the Callback Button block labeled Create structure with time data to load creates the time and signal data and uses it to build the structure with the required fields and hierarchy. The code starts by clearing any existing variable with the name simin which could be an object with property names that match the names for the fields of the structure.

clear simin;
sampleTime = 0.01;
numSteps = 1001;
time = sampleTime*(0:numSteps-1);
time = time';
data = sin(2*pi/3*time);
simin.time = time;
simin.signals.values = data;

To load the structure data, you can run the code to create the simin loading variable in the MATLAB Command Window or select then click the Create structure data to load Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.

When you load data for a multidimensional signal, add one more line of code to assign the signal dimensions to simin.signals.dimensions. When the signal data is scalar, set the dimensions field to 1, if you include it. When each signal value is a row vector, you can specify the dimensions as [1 n] or as n, where n is the number of elements in the row vector. When each signal value is an M-by|N| matrix, specify the dimensions field value as [M N].

When you want to load simulation input data without time data, use the Structure format, which does not include the time field and specify the desired sample rate using the Sample time parameter. For more information, see Control How Models Load Input Data.

Load Array Data

You can use the From Workspace block to load signal data formatted as an array, where the first column of the array contains time data and subsequent columns contain the signal data. When you load data in the array format, the sample values must be scalars or vectors.

The code for the Callback Button block labeled Create array data to load creates the time and signal data, concatenates the two row vectors into an array, and assigns the array to the simin loading variable.

sampleTime = 0.01;
numSteps = 1001;
time = sampleTime*(0:numSteps-1);
time = time';
data = sin(2*pi/3*time);
simin = [time,data];

To load the array data, you can run the code to create the simin loading variable in the MATLAB Command Window or select then click the Create 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 Workspace block supports loading a structure that contains timeseries objects or timetables as input data for a bus. Each timeseries object or timetable can contain any kind of data supported when loading data from a single timeseries object or timetable.

When you load input data for a bus using the From Workspace block, you must specify the Output data type setting 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 Workspace 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 bus data to load Callback button both define the SinusoidBus object.

A Bus object defines the bus hierarchy as well as the 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.

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;

clear busin;
busin.Cosine = timeseries(cosdata,time);
busin.SineBus.Sine = timeseries(data,time);
busin.SineBus.BigSine = timeseries(ampdata,time);

To load the bus data, you can run the code to create the busin loading variable in 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.

Load Partially Specified Bus Data

When you load input data for a bus using the From Workspace block, you do not need to provide data for every bus element. To partially specify input data for a bus, you can omit the corresponding field in the structure or specify its value as [].

The Create partially specified 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. It does not specify data for the Cosine bus element.

sampleTime = 0.01;
numSteps = 1001;
time = sampleTime*(0:numSteps-1);
time = time';

data = sin(2*pi/3*time);
ampdata = 2*data;

clear busin;
busin.Cosine = [];
busin.SineBus.Sine = timeseries(data,time);
busin.SineBus.BigSine = timeseries(ampdata,time);

To load the partially specified bus data, you can run the code to create the busin loading variable in the MATLAB Command Window or select then click the Create partially specified bus data to load Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.

When you partially specify data for a bus that includes a nested bus or nested array of buses, you can specify the entire nested bus or nested array of buses as [] and the From Workspace block provides ground values for all elements of the nested bus or array of buses. You can also partially specify data for an array of buses by omitting data for one or more of the buses contained in the array of buses.

Load Array of Buses Data

When you use the From Workspace block to load data for an array of buses, you must specify the Output data type parameter as the Simulink.Bus object that defines the buses the array of buses contains. All buses in an array of buses must be defined by the same Simulink.Bus object. The From Workspace block that loads data for the array of buses has its Output data type set to Bus: SinusoidBus. For details about defining the Bus object, see Load Bus Data.

The Create array of buses data to load Callback Button block creates an array that contains two structures of timeseries objects with hierarchy and field names that match the hierarchy and element names of the SinusoidBus object. The second structure, bus2, uses the same data as the first, shifted down by 1 so you can see all six signals on the Dashboard Scope block.

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;

bus1.Cosine = timeseries(cosdata,time);
bus1.SineBus.Sine = timeseries(data,time);
bus1.SineBus.BigSine = timeseries(ampdata,time);

bus2.Cosine = timeseries((cosdata-1),time);
bus2.SineBus.Sine = timeseries((data-1),time);
bus2.SineBus.BigSine = timeseries((ampdata-1),time);

busin = [bus1,bus2];

To load the array of buses data, you can run the code to create the busin loading variable in the MATLAB Command Window or select then click the Create array of buses data to load Callback Button block. Then, simulate the model and view the loaded data on the Dashboard Scope block.

See Also

Blocks

Objects

Related Topics