Effacer les filtres
Effacer les filtres

5x5 grid of black squares

3 vues (au cours des 30 derniers jours)
Oscar Tsang
Oscar Tsang le 4 Avr 2019
Commenté : Star Strider le 4 Avr 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

Réponse acceptée

Star Strider
Star Strider le 4 Avr 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 commentaires
Oscar Tsang
Oscar Tsang le 4 Avr 2019
Modifié(e) : Oscar Tsang le 4 Avr 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 le 4 Avr 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by