How to get columns as 'struct' in timetable

15 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-11-7
编辑:Stephen23 2023-11-7
"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)
T = 7×2 timetable
dt V S ___________ _______ __________ 01-Nov-2023 0.5018 1×1 struct 02-Nov-2023 0.80454 1×1 struct 03-Nov-2023 0.13943 1×1 struct 04-Nov-2023 0.57302 1×1 struct 05-Nov-2023 0.19045 1×1 struct 06-Nov-2023 0.69217 1×1 struct 07-Nov-2023 0.36403 1×1 struct

请先登录,再进行评论。

回答(2 个)

Peter Perkins
Peter Perkins 2023-11-10
编辑:Peter Perkins 2023-11-10
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!

Peter Perkins
Peter Perkins 2023-11-10
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)
T = 7×2 timetable
dt V S ___________ ________ __________ 01-Nov-2023 0.77627 1×1 struct 02-Nov-2023 0.080663 1×1 struct 03-Nov-2023 0.80134 1×1 struct 04-Nov-2023 0.57463 1×1 struct 05-Nov-2023 0.10883 1×1 struct 06-Nov-2023 0.32731 1×1 struct 07-Nov-2023 0.43739 1×1 struct
That works, for example
T.S(1:2).X
ans = 0.5949
ans = 0.9390
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 = 7×2 timetable
dt V S X Y ___________ ________ ___________________ 01-Nov-2023 0.77627 0.73699 0.31377 02-Nov-2023 0.080663 0.028832 0.9352 03-Nov-2023 0.80134 0.63014 0.79205 04-Nov-2023 0.57463 0.54658 0.74905 05-Nov-2023 0.10883 0.26599 0.91365 06-Nov-2023 0.32731 0.53686 0.67966 07-Nov-2023 0.43739 0.96268 0.65016
T.S(1:2,:)
ans = 2×2 table
X Y ________ _______ 0.73699 0.31377 0.028832 0.9352
T.S.X(1:2)
ans = 2×1
0.7370 0.0288
  3 个评论
Stephen23
Stephen23 2023-11-16
编辑:Stephen23 2023-11-16
"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?
Ludo Houben
Ludo Houben 2023-11-16
The shown screenshot is the data gathered on the PLC. Each column has the same amount of rows. This is guaranteed by the way the PLC stores the values. It stores each variable in a separate column. And each the datavalue of a PLC scan in the next row. So a struct is saved in multiple columns. The amount of variables and their size (double/uint16/bool) are known. That is why combining the columns into a struct is an option.
Regards
Ludo

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by