Borrar filtros
Borrar filtros

How to set just RowNames in a Matrix

46 visualizaciones (últimos 30 días)
Tiago Dias
Tiago Dias el 7 de Mzo. de 2018
Editada: Birdman el 7 de Mzo. de 2018
Hello, Let's assume I got a matrix 3x3
A = [1,2,3;4,5,6;7,8,9];
A.Properties.RowNames = {'row1','row2','row3'};
I got an error Field Assignment to a non-structure array object.
I just wanted to place names in rows and not to create a table. I want to keep as a double if possible.

Respuesta aceptada

Birdman
Birdman el 7 de Mzo. de 2018
Editada: Birdman el 7 de Mzo. de 2018
Since you do not want to use table, which is really smart to use, I would suggest you to use cell array.
A=mat2cell(A,ones(1,size(A,1)),ones(1,size(A,2)));
A(:,2:end+1)=A(:,1:end);
A(:,1)={'row1','row2','row3'};
and it is easy to reach numeric content of the cell by typing
Anum=cell2mat(A(:,2:end))
  4 comentarios
Tiago Dias
Tiago Dias el 7 de Mzo. de 2018
and it is possible to turn de variable names off? it shows me A1 A2 A3 etc
Birdman
Birdman el 7 de Mzo. de 2018
Editada: Birdman el 7 de Mzo. de 2018
It is not. It requires at least one nonempty character. And also as Stephen said below, array2table is also efficient and serves for you.
B=array2table(A,'RowNames',{'row1','row2','row3'})

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 7 de Mzo. de 2018
Editada: Stephen23 el 7 de Mzo. de 2018
"It creates a table 3x1, and not a table 3x3 that was my goal"
So use the correct function to create it: array2table
This will create a table of the size that you want.
Using a table will be much simpler than forcing a "header" into a cell array and splitting numeric data into separate cells of a cell array. A table is the correct data class if you want a header.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by