Assume you have a cell array with channel names and a mXn array of data where the first column is the time vector and the other columns represent the data in the same order as the values in the cell array.
The main part of this is creating the simulink bus object with the same names as the data and creating a structure of individual timeseries objects with the same names. This is the part I had trouble with finding from the documentation.
The advantages of this are:
- Makes it easy to select specific channels in the "FromWorkspace" block in Simulink
- If you have additional information like units you can encode that into the timeseries objects (as well as the simulink bus object).
This is example code that you can copy into Matlab and run:
load count.dat
timedata = [1:24]';
count = [timedata count];
clear timedata;
chan_title = {'chan1', 'chan2','chan3'}; % make sure no spaces between words
%% create simulink bus and timeseries structure
rundata_bus = Simulink.Bus;
rundata_ts = struct;
for i = 1:length(chan_title)
%% create bus elements
saveVarsTmp{1}(i, 1) = Simulink.BusElement;
saveVarsTmp{1}(i, 1).Name = chan_title{i};
%% create individual timeseries
rundata_ts.(chan_title{i}) = timeseries(count(:,i),count(:,1),'name',chan_title{i});
end
rundata_bus.Elements = saveVarsTmp{1};
clear saveVarsTmp;