Concatenate multiple tables with different variable names vertically to a big table
28 次查看(过去 30 天)
显示 更早的评论
Hi,
I have 20 tables in the matlab workplace with the the same dimensions (1000x8). However, the variable names are not the same. How can i concatenate the 20 tables vertically? I do not need the variable names in the big table, I'm just interest in the values within the 20 tables...Please note that the first column is datetime in each table. This, among other aspects, creates difficulties for me. The datetimes are not necessary either.
Helpful for any advice.
Kind regards
7 个评论
Stephen23
2023-11-18
编辑:Stephen23
2023-11-18
One way around this is to call TABLE2STRUCT with ToScalar=true (thus keeping the table columns/variables together), then STRUCT2CELL. I guess due to copy-on-write the conversion won't even copy any data in memory.
X = rand(1000000,10);
T = array2table(X);
C = struct2cell(table2struct(T,'ToScalar',true));
whos
Given that tables apparently store the column/variable data in a cell array it would be nice to have a way to access that cell array efficiently, e.g. to have ToRowVector option for TABLE2CELL (much like TABLE2STRUCT has the ToScalar option). And also the corresponding FromRowVector for CELL2TABLE.
Peter Perkins
2023-11-20
If you find yourself needing functions like these, you may be doing something unnecessary. In this case, the much more straight-forward solution is to just set the variables names all the same.
采纳的回答
Voss
2023-11-15
You can put all those tables into a cell array, then make all the tables in the cell array have the same set of variable names, and finally vertically concatenate the tables in the cell array into a single table.
Here's an example with two tables that have 5 variables each with different names.
% create two tables with 5 variables each:
Nvar = 5;
T1 = array2table(magic(Nvar))
T2 = array2table(eye(Nvar),'VariableNames',"OtherVar"+(1:Nvar))
% put the tables in a cell array C:
C = {T1,T2};
% pick some new variable names (I'm using "1" to "5"):
new_names = string(1:Nvar);
% rename the variables in each table in C:
C = cellfun(@(t)renamevars(t,t.Properties.VariableNames,new_names),C,'uni',0);
% vertically concatenate the contents of C into one table:
T = vertcat(C{:})
更多回答(1 个)
Steven Lord
2023-11-15
Are you trying to combine rows of those tables based on the date and time information stored in the first variable? If so I would try using table2timetable to turn those tables into timetables then use synchronize to combine the timetables based on their row times.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!