Timetables inside table are not updating correctly
Show older comments
Hello,
I'm trying to fill a table with timetables and scalar values (for Diagnostic feature Designer APP input) and I'm having some troubles defining the table.
I 'm using the following to create the table; where MatStruct is a structure (output of dir()) that contains the directories and filenames of the .mat files that I want to load and read.
sz = [length(MatStruct),4];
varTypes = {'timetable','timetable','timetable','double'};
DataTable = table('size',sz,'VariableNames',{'channel1','channel2','channel3','ConditionVar'},'VariableTypes',varTypes);
At this point the table is created properly, so I start to load and read the actual data onto the table:
for i=1:length(MatStruct)
load(fullfile(MatStruct(i).folder,MatStruct(i).name))
dt = diff(TIME);
dt = dt(1);
ch1 = array2timetable(channel1,'SampleRate',1/dt);
ch2 = array2timetable(channel2,'SampleRate',1/dt);
ch3 = array2timetable(channel3,'SampleRate',1/dt);
DataTable(i,1) = {ch1};
DataTable(i,2) = {ch2};
DataTable(i,3) = {ch3};
DataTable(i,4) = {ConditionVar};
end
For some reason, the first 3 columns (timetable columns) of the table are not being updated with ch1, ch2 and ch3; they keep the inital 1x0 timetable that appeared once the table was created. The fourth column scalar values, 'ConditionVar', is working fine (code not shown).
ans =
table
channel1
_______________
[1×0 timetable]
Whereas, if create the table as
DataTable = table()
The table ends up with the correct values, but I can't name table's variables so it is a bit messy when using the APP.
ans =
table
Var1
__________________
{5383×1 timetable}
I would like to understand why my created timetables are not being updated onto the table.
Thank you very much,
4 Comments
dpb
on 14 Jun 2021
Why are you trying to put timetables inside tables, to begin with????
Are there disparate times in these timetables, and if so what is the intent in the usage in putting them into one table? If they are separate tests for somesuch, I'd think probably more logical would be to create an array of timetables instead rather than multiple timetables inside a table.
BUT, if you do this, the variable name in the table will be that of the timetable and will return the timetable object--you then have to reference into that timetable with whatever are the actual variables in the timetable.
Seems like I did something like that once accidentally not realizing it was even possible; I'll have to puzzle out the syntax for references again as I don't recall just how that went.
While it was cute in some ways to see a nested table structure, it wasn't anything really of in help at that time so I didn't play with it much; without an explanation for the intended use here I really don't see why to do it here, either.
dpb
on 14 Jun 2021
But, I happened to still have in memory at timetable from a previous Answer, so--
> whos T*
Name Size Bytes Class Attributes
TT 289x1 5614 timetable
TT_15 8760x2 211438 timetable
TT_HHWL15 12x4 2124 timetable
TargetSheet 1x1 handle
Tw 1x1 8 double
>> T=table(TT);
>> head(T)
ans =
8×1 table
TT
Time Weight
______________________________
01-Jan-2021 00:00:00 1348.8
01-Jan-2021 00:15:00 1356.3
01-Jan-2021 00:30:00 1354.6
01-Jan-2021 00:45:00 1350.1
01-Jan-2021 01:00:00 1341.4
01-Jan-2021 01:15:00 1367.7
01-Jan-2021 01:30:00 1382
01-Jan-2021 01:45:00 1384.2
>> ans.TT.Weight
ans =
1.0e+03 *
1.3488
1.3563
1.3546
1.3501
1.3414
1.3677
1.3820
1.3842
>>
Yeah, that reminded me -- you dereference the variables similarly as to a nested stuct--just keep stringing dot names together to get where need to go.
Remember that you've encapsulated the timetable inside the variable in the table...whether that's a good idea or not would need to know, as noted above, just what the intent is and what the content of these various timetables is.
I would still think it's likely not the most appropriate storage layout, but could be convinced if can make a case for the why, other than just not doing what you really intended to do like my earlier example.
pcm314
on 15 Jun 2021
dpb
on 15 Jun 2021
With R2020b here I can't find any syntax that will let me enter a timetable of multiple rows into a table as a collapsed variable with only one auxiliary variable the way that example shows.
Which release do you have/does the sample app require? I didn't go searching, not having that app, but is there another page that goes with it that shows how that sample dataset was created?
Whatever I do, I run into the problem
>> T=table('Size',[1,2],'VariableTypes',{'timetable','double'})
T =
1×2 table
Var1 Var2
_______________ ____
[1×0 timetable] 0
>> T(1,1)=tmp
To assign to or create a variable in a table, the number of rows must match the height of the table.
>> whos tmp
Name Size Bytes Class Attributes
tmp 8x1 1118 timetable
>> T=table(tmp,0,'Size',[1,2],'VariableTypes',{'timetable','double'})
Error using table (line 233)
All table variables must have the same number of rows.
>>
It will create a composite datatable type, but can't store a real timetable into it; if embed the timetable as above it is contained in the table as a variable but still of the same height.
I don't see how to build the table they show, sorry...
Accepted Answer
More Answers (0)
Categories
Find more on Manage System Data 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!