필터 지우기
필터 지우기

How do you replace row names with a column?

조회 수: 60 (최근 30일)
Lina J
Lina J 2023년 4월 10일
답변: Joe Vinciguerra 2023년 4월 10일
say I am using matlab to read an excel file called test.xls, and the file reads into a table like this:
| names Var.1 Var.2 Var.3
1 | A 1 5 0
2 | B 2 4 1
3 | C 3 3 2
4 | D 4 2 3
5 | E 5 1 4
what do I need to do to turn the test.xls table into this:
names Var.1 Var.2 Var.3
A 1 5 0
B 2 4 1
C 3 3 2
D 4 2 3
E 5 1 4
  댓글 수: 1
the cyclist
the cyclist 2023년 4월 10일
Can you upload the test.xls file, and the code you used to import it into MATLAB? You can use the paper clip icon in the INSERT section of the toolbar.

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

답변 (2개)

Joe Vinciguerra
Joe Vinciguerra 2023년 4월 10일
T = array2table(magic(3));
Names = {'A'; 'B'; 'C'};
T.Names = Names;
T = movevars(T, "Names", "Before", "Var1");
% Here's your table
T
T = 3×4 table
Names Var1 Var2 Var3 _____ ____ ____ ____ {'A'} 8 1 6 {'B'} 3 5 7 {'C'} 4 9 2
% Label the rows using the column T.Names
T.Properties.RowNames = T.Names
T = 3×4 table
Names Var1 Var2 Var3 _____ ____ ____ ____ A {'A'} 8 1 6 B {'B'} 3 5 7 C {'C'} 4 9 2
% remove the "Names" column
T = removevars(T, "Names")
T = 3×3 table
Var1 Var2 Var3 ____ ____ ____ A 8 1 6 B 3 5 7 C 4 9 2
% Rename the row dimension to "Names"
T.Properties.DimensionNames(1) = {'Names'}
T = 3×3 table
Var1 Var2 Var3 ____ ____ ____ A 8 1 6 B 3 5 7 C 4 9 2
T.Names
ans = 3×1 cell array
{'A'} {'B'} {'C'}

Cris LaPierre
Cris LaPierre 2023년 4월 10일
No, there is not. You can rename the row names, but you cannot remove them from a table.
T = readtable('test.xls')
T.Properties.RowNames

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by