5x5 grid of black squares

조회 수: 6 (최근 30일)
Oscar Tsang
Oscar Tsang 2019년 4월 4일
댓글: Star Strider 2019년 4월 4일
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개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by