Adressing a cell

2 visualizaciones (últimos 30 días)
Jared
Jared el 20 de Oct. de 2011
I ran into an issue today and got it sorted out, but I want to understand what the reasoning behind it is.
I have a 1x10 cell named Rx{n},n=1:10. For simplicity's sake, lets just say the cells are filled sequentially 1-10.
Rx(5)=5
Also:
Rx{1,5}=5
I ran into an issue when I was trying to use the values in a legend.
legend(Rx(1),Rx(2)...) -Does not work
legend(Rx{1,1},Rx{1,2}...) -Does work
Since it is not a cell array, and each cell just contains 1 value, why aren't these equivalent?

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Oct. de 2011
If you are able to assign a numeric value to Rx(5) then at that time Rx must logically contain a numeric array. If later you attempt to assign to Rx{1,5} then MATLAB will complain that you are attempting to assign to cell array contents of a non-cell array. The only way you would have been able to assign to Rx{1,5} is if you cleared Rx first or you overwrote all of Rx in the assignment, effectively clearing it, leaving just a cell array.
Note that if you have a cell array with Rx{1,5} = 5 then Rx(1,5) is not the numeric value 5 but rather the 1x1 cell array {5}
If you assign sequentially to Rx{1}, Rx{2} and so on, then Rx will become a row vector of cells, and Rx{1} will in that case mean the same thing as Rx{1,1} and Rx{5} would mean the same thing as Rx{1,5} . This is the dual indexing feature of MATLAB, where any array element may be indexed by its full index list, or by its simple numeric index relative to the top-left of the array, with the simple numbering like
1 7 13
2 8 14
3 9 15
4 10 16
5 11 17
6 12 18
In this example, indexing the array at (3,3) would be the same as indexing the array at (15)

Más respuestas (1)

the cyclist
the cyclist el 20 de Oct. de 2011
In the case that works, you are using curly brackets, which means you are referring to the contents of the cell, which is presumably an appropriate input to the legend() function.
In the case that does not work, you are using parenthesis, which means you are referring to the cell itself (not the contents of the cell), and that is not a valid input to the legend() function.

Categorías

Más información sobre Matrix Indexing 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