MATLAB: Adressing a cell

cellcell arrays

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 1×10 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?

Best Answer

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)