Simple for/while loop question

Is there an easy way to loop this. The first argument of a() has to be looped but the second should stay the same.
text(a(1,5),a(1,6),'1');
rectangle('position', [a(1,5) a(1,6) 50 50]);
text(a(2,5),a(2,6),'2');
rectangle('position', [a(2,5) a(2,6) 30 30]);
text(a(3,5),a(3,6),'3');
rectangle('position', [a(3,5) a(3,6) 30 30]);

5 件のコメント

Geoff Hayes
Geoff Hayes 2016 年 3 月 31 日
Please clarify how the looping should handle the different size, 50x50 vs 30x30. Also, why is m used for the third rectangle (instead of a)?
a s
a s 2016 年 4 月 3 日
Hi sorry, the m should be an a, I've corrected my mistake. I wanted the sizes to vary for each rectangle (though this is not important it can stay the same)
per isakson
per isakson 2016 年 4 月 3 日
編集済み: per isakson 2016 年 4 月 3 日
"Is there an easy way to loop this." &nbsp I guess no! Had it been only the indicies of a that varied, but now there is also the strings, '1','2',3', and the numbers, 50 50, 30 30, 30 30, to worry about. One may put all that varies into a cell or struct array and loop over it. However, that might not meet the requirement easy. I sometimes do that to increase the readability of the code.
Star Strider
Star Strider 2016 年 4 月 3 日
Parenthetically, rather than drawing a rectangle around the text object (that seems to be what you’re doing), use an annotation object, specifically Create Text Box Annotation.
per isakson
per isakson 2016 年 4 月 3 日
Is this another example of a XyProblem? Is it about the for-loop in Matlab or about numbers in boxes?

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

回答 (1 件)

Image Analyst
Image Analyst 2016 年 4 月 3 日

0 投票

Try this:
% Create sample x,y coordinates and rectangle sizes
a = 70*rand(3,6)
rectSizes = [50,30,30];
% Plot them.
for k = 1 : length(rectSizes)
% Create a string to annotate with.
message = sprintf('%s', k+'0')
text(a(k,5),a(1,6), message, 'FontSize', 18, 'Color', 'b');
% Place the rectangle.
rectangle('Position', [a(k,5) a(k,6) rectSizes(k), rectSizes(k)], 'EdgeColor', 'r', 'LineWidth', 2);
end
grid on;

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

a s
2016 年 3 月 31 日

回答済み:

2016 年 4 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by