5x5 grid of black squares

3 次查看(过去 30 天)
I'm trying to lot a 5x5 grid of black boxes. I know i have to use a loop and but having trouble filling the boxes and get the printed on a figure. The code i have so far is:
for i = 1:25
handles = zeros(25,1);
x=mod(i,5);
y=floor(1/5);
handles(i) = fill(x,y, 5,5,'k');
end

采纳的回答

Star Strider
Star Strider 2019-4-4
I am not certain what you want.
Try this:
blksqrx = [0 1 1 0];
blksqry = [0 0 1 1];
figure
hold all
for k1 = 1:5
for k2 = 1:5
patch(blksqrx+(k1-1)*1.1, blksqry+(k2-1)*1.1, 'k')
end
end
hold off
axis tight
This separates them. If you want them to be contiguous, use this patch call instead:
patch(blksqrx+(k1-1), blksqry+(k2-1), 'k')
Experiment to get the result you want.
  2 个评论
Oscar Tsang
Oscar Tsang 2019-4-4
编辑:Oscar Tsang 2019-4-4
I have written pseudocode of what i am wanting to do but can't seem to figure out the "handles(i)= fil()" to make a 5x5 grid of black boxes
Star Strider
Star Strider 2019-4-4
You can get the handles to each patch object easily enough.
Substitute this line for my original patch call:
h((k1-1)*5+k2) = patch(blksqrx+(k1-1)*1.1, blksqry+(k2-1)*1.1, 'k');
That will give you all 25 of them.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by