필터 지우기
필터 지우기

assign name row to table

조회 수: 131 (최근 30일)
Luca Re
Luca Re 2023년 5월 31일
댓글: Walter Roberson 2023년 6월 1일
Sistemi_check=table(string(K_Nome),K_idxInstrument');
LastName = {"Sanchez";"Johnson"};
Sistemi_check.Properties.RowNames=LastName;
>> check_Instrument
Error using .
The RowNames property must be a string array or a cell array, with each name containing one or more
characters.
Error in check_Instrument (line 22)
Sistemi_check.Properties.RowNames=LastName;
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 5월 31일
You are trying to assign exactly two row names, but there needs to be one row name entry for each row.
You have at least 30 rows in your table, so you need at least 30 row-name entries.
Luca Re
Luca Re 2023년 5월 31일
편집: Walter Roberson 2023년 6월 1일
ok i want to rename columns in a table..is correct to use VariableNames? i try it but i get an error
Sistemi_check=table(string(K_Nome),K_idxInstrument');
LastName = {"Sanchez";"Johnson"};
Sistemi_check.Properties.VariableNames=LastName;
Error using .
The VariableNames property is a cell array of character vectors. To assign multiple variable names,
specify nonempty names in a string array or a cell array of character vectors.
Error in check_Instrument (line 22)
Sistemi_check.Properties.VariableNames=LastName;

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

답변 (2개)

Walter Roberson
Walter Roberson 2023년 5월 31일
Either use
LastName = ["Sanchez";"Johnson"];
Sistemi_check.Properties.RowNames=LastName;
or else
LastName = {'Sanchez';'Johnson'};
Sistemi_check.Properties.RowNames=LastName;
That is you can use a cell array of character vectors, or you can use a string() array, but you cannot use a cell array of string() objects.
  댓글 수: 3
VBBV
VBBV 2023년 6월 1일
@Luca Re, use the cell array of character vectors as shown below
% use the cell array as shown below
LastName = {'Sanchez','Johnson'};
Sistemi_check.Properties.RowNames=LastName;
By default, RowNames variable refers to rows in a table. So , a ; is not needed to tell table to concatenate the names vertically. See more about RowNames property RowNames
Walter Roberson
Walter Roberson 2023년 6월 1일
Sistemi_check = table("first", 123)
Sistemi_check = 1×2 table
Var1 Var2 _______ ____ "first" 123
LastName = ["Sanchez";"Johnson"];
Sistemi_check.Properties.VariableNames=LastName
Sistemi_check = 1×2 table
Sanchez Johnson _______ _______ "first" 123
Sistemi_check = table("first", 123)
Sistemi_check = 1×2 table
Var1 Var2 _______ ____ "first" 123
LastName = {'Sanchez';'Johnson'};
Sistemi_check.Properties.VariableNames=LastName
Sistemi_check = 1×2 table
Sanchez Johnson _______ _______ "first" 123

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 5월 31일
Here is a plain example how to rename row and column names in a table array:
t1 = date;
t2 = '01-June-2023';
t3 = '10-June-2023';
t4 = '20-June-2023';
T = {t1; t2; t3;t4};
D = table(T);
D.N = [10:10:40]'
D = 4×2 table
T N ________________ __ {'31-May-2023' } 10 {'01-June-2023'} 20 {'10-June-2023'} 30 {'20-June-2023'} 40
% Add row names:
LastName = {'Sanchez';'Johnson'; 'Brown'; 'Green'};
D.Properties.RowNames=LastName
D = 4×2 table
T N ________________ __ Sanchez {'31-May-2023' } 10 Johnson {'01-June-2023'} 20 Brown {'10-June-2023'} 30 Green {'20-June-2023'} 40
% Change column names:
Col_Name = {'Date', 'Number'};
D=renamevars(D,{'T', 'N'}, Col_Name)
D = 4×2 table
Date Number ________________ ______ Sanchez {'31-May-2023' } 10 Johnson {'01-June-2023'} 20 Brown {'10-June-2023'} 30 Green {'20-June-2023'} 40

카테고리

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