Not able to excecute the code

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.

 Respuesta aceptada

Aravind Ravikumar
Aravind Ravikumar el 21 de Jun. de 2019
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 comentarios

Rajeev Kumar
Rajeev Kumar el 3 de Jul. de 2019
Thanks very much for correcting the code. However I have stuck in one implementation where I want to create a table with just column and row name. I am trying in the following way
Step1 : tab1 = cell2table(...) ; tab2=cell2table(...) ;tab3=cell2table(...)
Step2 : table = [tab1 tab2 tab3]
step3 : table..Properties.RowNames = x_name ; x_name is a cell array of specied element (say it is of size 1x20)
This is throwing the following error : The RowNames property must contain one name for each row in the table. Can I create a table with only rows and column name?
Walter Roberson
Walter Roberson el 3 de Jul. de 2019
No. You must have at least one column of value along with the row name, and that column must have as many rows as you have row names.
Walter Roberson
Walter Roberson el 3 de Jul. de 2019
Further testing shows that to be incorrect: You can have rownames even with now columns.
What is showing up as size(table) ?
Rajeev Kumar
Rajeev Kumar el 4 de Jul. de 2019
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?
Walter Roberson
Walter Roberson el 4 de Jul. de 2019
With 0 rows you must have 0 row names.

Iniciar sesión para comentar.

Más respuestas (1)

Akshay Malav
Akshay Malav el 21 de Jun. de 2019
Editada: Akshay Malav el 21 de Jun. de 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 comentario

Walter Roberson
Walter Roberson el 21 de Jun. de 2019
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.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 21 de Jun. de 2019

Comentada:

el 4 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by