MATLAB: Not able to excecute the code

addition of row names in a tablematalb

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.

Best 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;