how to easily and quickly change the variable names (headers) of multiple tables

조회 수: 36 (최근 30일)
Hi,
I have 20 tables, let's call them A,B,C.....etc. with size of 1000 lines and 40 column.
I would like to concatenate them all in dimension 1, using function
cat(1,A,B,C.....etc)
the problem is that I must assign each table the same Headers (variable names) to each column, to be able to concanenate them all. Right now, they are all different, because the Headers were assigned by MATLAB somewhere in my code.
Right now, the code I have is
A.Properties.VariableNames{1}='Header1'
and the code above works perfectly for assigning the variable name 'Header1' to the column 1 of Table A.
However, I am pretty confident that there is a way to automate this for every case (20 tables and 40 columns), without the need to write 800 lines of code (20*40), which obviously makes no sense. Any help is appreciated.
Thank you very much,
Best regards,

채택된 답변

Walter Roberson
Walter Roberson 2022년 3월 11일
A.Properties.VariableNames can be assigned a cell array of character vectors to change all of the names. This would reduce down to 20 lines.
Alternatively, table2cell all the tables, vertcat the result, cell2table with 'VariableNames' option
  댓글 수: 1
Jon
Jon 2022년 3월 11일
In particular you can assign
B.Properties.VariableNames = A.Properties.VariableNames
C.Properties.VariableNames = A.Properties.VariableNames
% etc
Better if you had an array of tables you could do this in a loop.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2022년 3월 11일
For each table, it can be done like this: (To illustrate, this example is just for 5 columns)
T = array2table(rand(3,5));
vn = repmat("Header", 1,5) + string(1:5)
vn = 1×5 string array
"Header1" "Header2" "Header3" "Header4" "Header5"
T.Properties.VariableNames = vn
T = 3×5 table
Header1 Header2 Header3 Header4 Header5 _______ ________ _______ _______ _________ 0.7791 0.74276 0.25609 0.73802 0.29442 0.36951 0.059274 0.61946 0.45536 0.911 0.78598 0.3063 0.44896 0.16828 0.0066547

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by