Using timetable versus tscollection objects

조회 수: 3(최근 30일)
Alan Bindemann
Alan Bindemann 2020년 11월 16일
댓글: Alan Bindemann 2020년 11월 23일
I have Simulink models that log signal data using the Simulink.SimulationData.Dataset class.
I'm writing post-processing m-code that helps analyze model outputs and would like to combine the data into either a tscollection object or a timetable object, which is easier to deal with than the collection of Simulink.SimulationData.Signal objects contained in the logsout object generated by the Simulink model.
There doesn't seem to be an 'easy' way to convert the signals in logsout into a timetable object, whereas one can create a tscolleciton object rather easily:
tsCol = tscollection;
for ii=1:logsout.numElements
tsCol = tsCol.addts(logsout.getElement(ii).Values);
MathWorks seems to really be pushing the use of timetables through functions such as stackedplot, and tscollection objects seem to be something of an afterthought.
It there an easy way to convert a tscollection object into a timetable? Failing that, is there a way to create an empty timetable and add the signal data from the Dataset signals in a loop as shown above?


Corey Silva
Corey Silva 2020년 11월 20일
Hi Alan,
Below is a toy example where you have a timeseriescollection containing two timeseries, ts1 and ts2.
>> ts1 = timeseries(rand(5,1),'Name','ts1');
>> ts2 = timeseries(rand(5,1),'Name','ts2');
>> tsc = tscollection({ts1,ts2});
>> tt = timetable(seconds(tsc.Time),tsc.ts1.Data(:),tsc.ts2.Data(:),'VariableNames',{'ts1_Data','ts2_Data'})
tt =
5×2 timetable
Time ts1_Data ts2_Data
_____ ________ ________
0 sec 0.15761 0.14189
1 sec 0.97059 0.42176
2 sec 0.95717 0.91574
3 sec 0.48538 0.79221
4 sec 0.80028 0.95949
Hopefully this gives you enough to get started with using timetables. I would like to assure you that we are actively working on solutions to converting between timeseries objects and timetables for future releases of MATLAB.
  댓글 수: 3
Alan Bindemann
Alan Bindemann 2020년 11월 23일
Thanks, That helps a lot. The code below is close to doing what I want.
for ii=1:logsout.numElements
elemTs = logsout.getElement(ii).Values;
if ~isempty(elemTs.DataInfo.Units)
Units{ii} = elemTs.DataInfo.Units.Name;
Units{ii} = '';
Names{ii} = elemTs.Name;
if ii==1
tTable = timetable(seconds(elemTs.Time), elemTs.Data);
tTable = addvars(tTable, squeeze(elemTs.Data));
tTable.Properties.VariableUnits = Units;
tTable.Properties.VariableNames = Names;
I recommend that the MathWorks mentions the addvars and removevars functions in the documentation for timetable.
Alan Bindemann

댓글을 달려면 로그인하십시오.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by