Clear Filters
Clear Filters

5x5 grid of black squares

3 views (last 30 days)
Oscar Tsang
Oscar Tsang on 4 Apr 2019
Commented: Star Strider on 4 Apr 2019
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

Accepted Answer

Star Strider
Star Strider on 4 Apr 2019
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 Comments
Oscar Tsang
Oscar Tsang on 4 Apr 2019
Edited: Oscar Tsang on 4 Apr 2019
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 on 4 Apr 2019
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!