How to get columns as 'struct' in timetable
26 views (last 30 days)
Show older comments
Hi all,
I'm 'playing' around with timetable for adding logdata to use in a simulink model but I'm strugling with the 'struct in struct' structure. Currently we import the data via an external script and this creates the desired 'struct in struc' structure with all variables as timeseries. When I want to display the data, then a warning is shown "Timeseries will not open in the timeseries viewer in a future release" and on several occacions I saw that timetable is 'preferred'. Possible that the timeseries will be removed in future releases, that is why I am now looking for the posibilities of the timetable. However I'm struggling with the 'struct in struct' structure.
The data is gathered by a PLC which contains several functionblocks, each containting its own set of structs (in structs). Here is my attempt of importing the data. It looks already promissing, but the 'blue' marked columns are part of a struct. When I want to acces the data, then I want to be able to couple the complete struct to an input of my model. I.e. an input "i_CFG" of a function should be coupled to "TestArray.stTcComDevDetProbes_CFG". At this stage this is not possible. How do I have to change the timetable that the mentioned columns are marked as a 'struct'? Or is this not possible? In that case, are there other solutions next to creating 'struct in struct' timeseries?
Thanks for any reply
Ludo
2 Comments
Stephen23
on 7 Nov 2023
Edited: Stephen23
on 7 Nov 2023
"How do I have to change the timetable that the mentioned columns are marked as a 'struct'? Or is this not possible?"
It is possible. In general the columns of a (time)table can be of any class (as long as each column/variable has the same number of rows as the table). For example, all of these columns/variables have size 7x1:
dt = datetime(2023,11,1:7).';
V = rand(7,1);
S = struct('X',num2cell(rand(7,1)),'Y',num2cell(rand(7,1)));
T = timetable(dt,V,S)
Answers (2)
Peter Perkins
on 10 Nov 2023
Edited: Peter Perkins
on 10 Nov 2023
For the record, you are correct that timetable is the recommended way to work with timestamped data, but there are no plans to actually remove timeseries. That being said, timetable is the future, and that's where future enhancements will be added. In the long run (and maybe even the short run), you will be happier with timetable. If there are things you can't do with timetable that you can with timeseries, it would be good to post them.
As you seem to have done! Thanks!
0 Comments
Peter Perkins
on 10 Nov 2023
Ludo, I'm not 100% sure I follow your question, but lemme take a shot.
As Stephen shows, a variable in a table can be a struct array
dt = datetime(2023,11,1:7).';
V = rand(7,1);
S = struct('X',num2cell(rand(7,1)),'Y',num2cell(rand(7,1)));
T = timetable(dt,V,S)
That works, for example
T.S(1:2).X
but the display is not great, and in any case you might be happier with this:
S = table(rand(7,1),rand(7,1),VariableNames=["X" "Y"]);
T = timetable(dt,V,S)
T.S(1:2,:)
T.S.X(1:2)
3 Comments
Stephen23
on 16 Nov 2023
Edited: Stephen23
on 16 Nov 2023
"So I want to 'combine' columns 7 and 8 into one struct."
If you want to convert some numeric vectors into a structure array than NUM2CELL, concatenation, and CELL2STRUCT could help.
"The struct size is known"
This is very confusing: as I wrote in my comment, the structure array must have the same number of rows as the table has. Is your "known" struct size guaranteed to have that many rows? What should happen if it doesn't ?
Lets be specific: your screenshot shows a table with 173400 rows. Does your "known" structure array size have that many rows?
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!