Question

I have a 10000x43 array that represents test data taken from a data acquisition hardware. The first column is the time vector and the remaining columns are each channel. I also have a 1x43 cell array that defines each channel's name.

I would like to create a timeseries object from this array data. The reason I want to do this is so that I can use the channel names in a bus selector block - this makes it easy to feed the test data into a simulink model.

I have looked online and the documentation but I have not had much luck on how to do reproduce the same type of timeseries object I get when using the "ToWorkspace" block in simulink.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top