フィルターのクリア

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
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
Name Size Bytes Class Attributes C 10x1 80001040 cell T 1000000x10 80002889 table X 1000000x10 80000000 double
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
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
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))
T1 = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
T2 = array2table(eye(Nvar),'VariableNames',"OtherVar"+(1:Nvar))
T2 = 5×5 table
OtherVar1 OtherVar2 OtherVar3 OtherVar4 OtherVar5 _________ _________ _________ _________ _________ 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
% 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{:})
T = 10×5 table
1 2 3 4 5 __ __ __ __ __ 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
  3 件のコメント
Vlatko Milic
Vlatko Milic 2023 年 11 月 15 日
I made it work, thanks again!
Voss
Voss 2023 年 11 月 15 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
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.

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by