MATLAB Answers

0

Working With Matlab Tables

Asked by Joe Dainton on 17 Nov 2019 at 17:48
Latest activity Edited by per isakson
on 17 Nov 2019 at 23:16
Hello all
Seems like a simple question but it driving me insane.
I have imported data from an excel sheet into a Table.
I just want to change the value of an element in the table, i remember from somewhere that i could use the matrix notation so i used the following command:-
Data(1,4) = 'Tony'
where Data is the table name, and i want row 1, column 4 to be changed to Tony
But i get the following error:-
>> Data(1,4)='Tony'
The number of table variables in an assignment must match.
Can i ask why this does not work?
Thank you

  0 Comments

Sign in to comment.

Tags

4 Answers

Answer by Image Analyst
on 17 Nov 2019 at 18:07

Since you want to change the CONTENTS of the table, use braces not parentheses.
Data{1,4}='Tony'

  1 Comment

Joe Dainton on 17 Nov 2019 at 18:36
Thank you for your response, however this does not work, i still get an error:-
HHHHHHHHHHHHHHH.JPG

Sign in to comment.


Answer by Steven Lord
on 17 Nov 2019 at 18:08

Use parentheses to extract a smaller table from a larger table or to assign a smaller table into a larger one.
Use curly braces to extract data from a table or store data into a table when the data is not itself a table.
Data{1, 4} = 'Tony'; is probably what you want, since 'Tony' is not itself a table.
See the sections "Tables Containing Specified Rows and Variables" and "Extract Data from Specified Rows and Variables" on this documentation page for examples of the differences between Data(...) and Data{...}.

  0 Comments

Sign in to comment.


Answer by Star Strider
on 17 Nov 2019 at 19:23

I created my own table to test my code. (I didn’t post it previously because the other two Answers had already appeared.)
The substitution only works in my code with this syntax:
Data(1,4) = {'Tony'};
Example illustration:
Chars = cell2table({'Abcd';'Efgh';'Ijkl';'Mnop';'Qrst';'Uvwxyz'},'VariableNames',{'Chars'}); % Create Table
Data = [array2table(rand(6,3)) Chars]; % Create Table
Data(1,4) = {'Tony'}; % Substitute Element
See if that works with your table.

  0 Comments

Sign in to comment.


Answer by per isakson
on 17 Nov 2019 at 22:14
Edited by per isakson
on 17 Nov 2019 at 23:16

"I remember from somewhere" With Matlab that doesn't work for me. I need to consult the documentation more often than not. This does the trick (on R2018b)
Data{1,4} = {'Tony'};
and this
Data.Customer(1) = {'Tony'};
"Why" The value of Data{1,4} is a cell.
>> class( Data{1,4} )
ans =
'cell'
Thus the new value need to be a cell. (My mental model.)
Star Strider shows that
Data(1,4) = {'Tony'};
works, which puzzles me. Is that documented behavior?

  0 Comments

Sign in to comment.