Concatenate multiple tables with different variable names vertically to a big table
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vlatko Milic
el 15 de Nov. de 2023
Comentada: Peter Perkins
el 20 de Nov. de 2023
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 comentarios
Stephen23
el 18 de Nov. de 2023
Editada: Stephen23
el 18 de Nov. de 2023
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
el 20 de Nov. de 2023
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.
Respuesta aceptada
Voss
el 15 de Nov. de 2023
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{:})
3 comentarios
Más respuestas (1)
Steven Lord
el 15 de Nov. de 2023
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 comentarios
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!