Timetables inside table are not updating correctly
2 views (last 30 days)
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 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
Cris LaPierre
on 15 Jun 2021
I think the issue is that you are treating tables like cells. In a cell, each can be a variable. However, in a table, an entire column represents a variable. You cannot, therefore, assign a table to a single element (row) of a column. It must be assigned to the entire column.
dt = 0.05;
channel1 = rand(3,2);
channel2 = rand(3,2);
channel3 = rand(3,2);
ch1 = array2timetable(channel1,'SampleRate',1/dt);
ch2 = array2timetable(channel2,'SampleRate',1/dt);
ch3 = array2timetable(channel3,'SampleRate',1/dt);
% Notice the variable names, ch1-3, across the top of the table
% The timetable variable names are displayed under the table variable names
% The timetable values are shown in their corresponding columns under ch1,
% ch2, and ch3
TT = table(ch1,ch2,ch3)
I think you need to rethink your approach for storing the data.
13 Comments
Cris LaPierre
on 15 Jun 2021
I agree. I cannot think of a scenario where it would work. In the feedback, I have also suggested they consider not allowing these data types for VarType.
More Answers (0)
See Also
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!