Looping through cell array in order

I'm drawing squares on a grid. The squares need to have an ID as set within a cell array. I was expecting the ID to loop through each ID in the cell in order by instead it cycles through in rows.
&nbsp
cell_id = cell(1, 2); % 1 x 5 cell array
cell_id{1, 1} = 1365;
cell_id{1, 2} = 1256;
cell_id{1, 3} = 6214;
cell_id{1, 4} = 2587;
cell_id{1, 5} = 1564;
hold on
for i = 1:2:12
count = 1;
for ii = 1:2:12
plot(1*i , 1*ii, 's', 'MarkerSize', 50);
if (count <= 5)
id = cell_id(1,count);
text(1*i, 1*ii, ['ID:',id]);
count = count + 1;
elseif (count > 5)
id = cell_id(1,1);
count = 1;
text(1*i, 1*ii, ['ID:',id]);
end
end
end
Any ideas, thanks.

1 件のコメント

per isakson
per isakson 2014 年 12 月 10 日
編集済み: per isakson 2014 年 12 月 10 日
  • Do you expect 4 rows times 6 columns of boxes?
  • Replace i by xx and ii by yy or something associated with directions
  • Set the axes limits before adding the boxes. It's confusing that the limits change all the time
  • And format the code properly. (I did it this time.)

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2014 年 12 月 10 日

0 投票

You appear to be a bit confused about a few things:
cell_id = cell(1, 2);
declares a 1 x 2 cell array, not a 1 x 5. Anyway, you could replace your first two lines with:
cell_id = {1365, 1256, 6214, 2587, 1564};
Secondly, you reset your count for each column, since it's set to one in the first for loop. You probably meant to do the assignment to 1 before the first loop.
Also, use more descriptive variables than i and ii. row and col would be better.
To sum it up:
figure; axis off; hold on;
cell_id = {1365, 1256, 6214, 2587, 1564};
count = 1;
for col = 1:2:12
for row = 1:2:12
plot(col, row, 's', 'MarkerSize', 50);
text(col, row, sprintf('ID:%d', cell_id{count}));
count = mod(count, numel(cell_id)) + 1;
end
end

7 件のコメント

Josh
Josh 2014 年 12 月 10 日
Got really confused, this is really neat/optimised. Thanks
Josh
Josh 2014 年 12 月 11 日
Guillaume, I just noticed if I change the amount of IDs to only three it just loops the same ID for each row again
cell_id = {1365, 1256, 6214};
If I have 4 it does change but misses a random ID out it only works fully on 5 IDs.
Any suggestions? Thanks
Guillaume
Guillaume 2014 年 12 月 11 日
編集済み: Guillaume 2014 年 12 月 11 日
Works fine for me with 3 and 4 IDs.
However, be aware of the coordinate system used by plot and text in my code (which is the same as in your original code). The IDs are printed starting at the bottom left corner of the figure, going up the column and then up the next column on the right and so on.
Rereading your original question it looks like you wanted to start in the upper left corner and go along the rows rather than the columns. You need to change the for loop accordingly:
for row = 11:-2:1
for col = 11:-2:1
Josh
Josh 2014 年 12 月 11 日
Hmm ... this code
figure; axis off; hold on;
cell_id = {1365, 1256, 6214};
count = 1;
for col = 12:-2:1
for row = 12:-2:1
plot(col, row, 's', 'MarkerSize', 50);
text(col, row, sprintf('ID:%d', cell_id{count}));
count = mod(count, numel(cell_id)) + 1;
end
end
Produces the same ID
Can you replicate this? Does it work for you?
Guillaume
Guillaume 2014 年 12 月 11 日
I wrote:
for row = 11:-2:1
for col = 11:-2:1
Note the order of the variables. row first, col second. With your code you go down the column, since there are 3*n rows, when you go back to the next column, your back to your first ID when there are only three of them.
If you swap row and col as above, you go along the rows and wrap back at the beginning of a row.
Image Analyst
Image Analyst 2014 年 12 月 11 日
I see no reason for a cell array at all. What about this requires cells instead of a simple numerical array?
Josh
Josh 2014 年 12 月 11 日
Oops, perfect thanks. Good explanation

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

タグ

質問済み:

2014 年 12 月 10 日

コメント済み:

2014 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by