Borrar filtros
Borrar filtros

5x5 grid of black squares

3 visualizaciones (últimos 30 días)
Oscar Tsang
Oscar Tsang el 4 de Abr. de 2019
Comentada: Star Strider el 4 de Abr. de 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

Respuesta aceptada

Star Strider
Star Strider el 4 de Abr. de 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 comentarios
Oscar Tsang
Oscar Tsang el 4 de Abr. de 2019
Editada: Oscar Tsang el 4 de Abr. de 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 el 4 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by