How do I change a string or number in a table?

조회 수: 2 (최근 30일)
Mathilde Engell
Mathilde Engell 2015년 1월 20일
답변: Peter Perkins 2015년 1월 22일
I have a table containing columms of codes, names and numbers.
How do I change a unit in the table?
I have tried grades{2,2}='Martin';
But this does not work for a table.
  댓글 수: 1
Image Analyst
Image Analyst 2015년 1월 20일
Which elements or columns contain the units? Are the units names all in column 2 of the table, and column 2 is a column of all strings?

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

답변 (1개)

Peter Perkins
Peter Perkins 2015년 1월 22일
Mathilde, I think with that syntax, you just need to wrap that string in curly braces in order to assign it into a variable that's a cell array of strings:
>> grades = table([1;2;3],{'Robin';'Sparrow';'Finch'},[70;80;90],'VariableNames',{'Code' 'Name' 'Number'})
grades =
Code Name Number
____ _________ ______
1 'Robin' 70
2 'Sparrow' 80
3 'Finch' 90
>> grades{2,2} = 'Martin'
RHS should have 1 columns.
>> grades{2,2} = {'Martin'}
grades =
Code Name Number
____ ________ ______
1 'Robin' 70
2 'Martin' 80
3 'Finch' 90
The idea is that grades{2,2} is the second element of the Names variable, and that is itself a 1x1 cell array, and so the right hand side of the assignment should be a 1x1 cell array. This is a bit confusing, because it involves braces for both the table and for the strings.
But there's a much simpler syntax:
>> grades.Name{2} = 'Oriole'
grades =
Code Name Number
____ ________ ______
1 'Robin' 70
2 'Oriole' 80
3 'Finch' 90
grades.Name(2) is a 1x1 cell array, but grades.Name{2} is the contents of that cell, in this case a string.
Hope this helps.

카테고리

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