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

2 views (last 30 days)
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 Comment
Image Analyst
Image Analyst on 20 Jan 2015
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?

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 22 Jan 2015
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.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!