MATLAB Answers

0

Not able to excecute the code

Asked by Rajeev Kumar on 21 Jun 2019
Latest activity Commented on by Walter Roberson
on 4 Jul 2019
I am trying to execute following codes :
rows={'A','B'};
data = cell(0,5);
T = cell2table(data);
T.Properties.RowNames = rows;
Matlab is throwing an error :The RowNames property must contain one name for each row in the table.
Can anybody comment if I am missing anything? I will appreciate any help.

  0 Comments

Sign in to comment.

2 Answers

Answer by Aravind Ravikumar on 21 Jun 2019
 Accepted Answer

Hey, cell(0,5) returns a 0x5 empty cell array. That is why row number isn't matching with rows variable. You should use You can simply create a cell array using the command,
data = {0,5}
Also, in this example data is a 1x2 cell array. Hence it has only one row. To change that row name, you could do something like,
rows={'A'};
data = {0,5};
T = cell2table(data);
T.Properties.RowNames = rows;
For creating a 2x1 cell array, you'll have to use
rows={'A','B'};
data = {0;5};
T = cell2table(data);
T.Properties.RowNames = rows;

  5 Comments

Further testing shows that to be incorrect: You can have rownames even with now columns.
What is showing up as size(table) ?
Thanks Walte size. Here is the result:
size(table)=0x3
h=table.Properties.VariableNames
h =
1×3 cell array
{'tab1'} {'t'ab2} {'tab3'}
Could you please comment on this?
With 0 rows you must have 0 row names.

Sign in to comment.


Answer by Akshay Malav on 21 Jun 2019
Edited by Akshay Malav on 21 Jun 2019

First change the number of rows from 0 to 1 and correspondingly the rows array should contain only 1 element either 'A' or 'B'. Run it , it will work fine
Here is the code
rows={'A'};
data = cell(1,5);
T = cell2table(data);
T.Properties.RowNames = rows;

  1 Comment

Right. You can only have a row name for a row that exists. You cannot put in placeholder row names for rows to be added later.

Sign in to comment.