Adressing a cell
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Walter Roberson
on 20 Oct 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)
0 Comments
More Answers (1)
the cyclist
on 20 Oct 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.
0 Comments
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!