gpuguy
gpuguy

Reputation: 4585

Drawing 256 squares/rectangles in MATLAB

I am trying to draw 256 small sized squares using MATLAB rectangle function. If I am drawing abut 10 squares then the following works fine:

for i=1:2:40

rectangle('Position',[5,3+i,0.3,0.3],...
      'Curvature',[0,0],...
     'LineStyle','-', 'faceColor', 'black')

end

axis off; 

daspect([1,1,1])

enter image description here

But when I change the last value of for loop to 512 (to draw 256 squares), it is not printing properly:

enter image description here

Here is the magnified version of a section of the above image: enter image description here

This image clearly shows some thing is wrong somewhere as the sides of the squares are not perfectly equal and that the squares are becoming smaller in size for higher no. of squares: Can anybody help me draw the squares perfectly with size not diminishing, ? (I don't have any issues with memory, and I can tolerate multiple pages scrolling down for covering entire squares )

Upvotes: 2

Views: 1481

Answers (2)

Andrey Rubshtein
Andrey Rubshtein

Reputation: 20915

It is a classical Moire effect. You can't show that much rectangles on your monitor, because there aren't enough pixels. Matlab does some down-sampling for you. Thus, you get another frequency, that did not exist originally.

Upvotes: 5

H.Muster
H.Muster

Reputation: 9317

I tried your code and it works fine even when the loop does 512 iterations - when you zoom-in in the final matlab figure. The artifacts you describe are probably caused by the monitor resolution or a low resolution while exporting to non-vector files.

Try to export your image as a vector file (eps or svg) to see that everything looks fine when you zoom in.

Upvotes: 3

Related Questions