3

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
ashah
  • 193
  • 1
  • 4
  • 15
  • 1
    You know the [`From Workspace`](http://www.mathworks.com/help/simulink/slref/fromworkspace.html) block lets you just plug in an array corresponding to simulation times and signals, right? In any case, the [MATLAB documentation](http://www.mathworks.com/help/matlab/ref/timeseriesclass.html) on the `timeseries` class looks pretty simple to use: `ts = timeseries(data, time);` – Dang Khoa Jun 14 '13 at 21:46
  • I know that. The reason I want to use a timeseries object as the input in the FromWorkspace block instead of a plain array is that the array can only be used for single dimension signals. For multidimension signals I have to use at least a structure (which doesn't store signal names) or separate arrays (means duplicate time series columns). Plus if I use a structure with all columns then I need to use a demux of the same size but if I use a timeseries with a bus I can select only the channels I want and not have to worry about the specific column. – ashah Jun 14 '13 at 21:50

1 Answers1

1

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;
ashah
  • 193
  • 1
  • 4
  • 15