Q:

distribute same size circles evenly inside a square using Matlab

I have a figure of size 14 x 14 square drawn inside an axis of 20 x 20, in matlab.
I am trying to draw circles of radius 0.7 inside the square and need to arrange them evenly. I need to draw 233 circles. Please let me know how can I do it? 
Currently I can draw them randomly but couldn't get 233 circle. Please see my below code. 
Your reply is appreciated.
% Urban, sub urban, Rural areas
      x_area =[3, 12, 6];
      y_area = [6, 8, 16];
      r_area = [1, 7, 2];

       f = figure;
       hAxs = axes('Parent',f);
        hold on, box on, axis equal
        xlabel('x')
        ylabel('y','Rotation',0)
        title('Compute the area of circles a vectorized way for several cicles')
        axis([0 20 0 20])
        rectangle('Position',[5,1,14,14])
         rectangle('Position',[3,1,2,2])
        rectangle('Position',[1,3,4,4])
         hold on, box on, axis equal

      a = 233;
      x_base_urban = randi([6 18], 1, a);
       b = rand([10 8], 1);
       y_base_urban = randi([2 14],1, a);
       r_base_urban = 0.9;

        size_x = size(x_base_urban);
        size_x = size_x(2);
        size_y = size(y_base_urban);
         size_y = size_y(2);

        colour = rand(size_x,3);
       for t = 1: size_x
         plot(x_base_urban(t)+ r_base_urban.*cos(0:2*pi/100:2*pi),...
        y_base_urban(t)+ r_base_urban.*sin(0:2*pi/100:2*pi),'parent',hAxs)
         plot(x_base_urban(t),y_base_urban(t),'+','parent',hAxs)

         end

Thanks

A:

Randomly plotting everything won't work. Actually, if your circles can't overlap, nothing will work. To show that, just compare the results of following calculations:
lSquare = 14;
rCircle = 0.7;
nCircles = 233;
areaCircles = nCircles * pi * rCircle^2
areaSquare = lSquare^2

You will see that areaCircles > areaSquare, so it is impossible to fit them all in. On the other hand, if areaSquare >= areaCircles does not guarantee you that a solution exists!
Try your setup with a smaller example to come up with a solution. E.g. take a square box and a bunch of spherical objects (balls, marbles, oranges, apples, ... if need be) and try to fit as much of those in your box. If that works, you might even want to draw their positions on a sheet of paper before trying to implement it.
If you do this correctly, you will get an idea of how to stack round objects in a square container. That is also exactly what you need to do in your exercise. Then try to make a model/algorithm of what you did manually and implement that in MATLAB. It won't be hard, but you will need some small calculations: Pythagoras and intersection of circles.
I also suggest you use a function to draw a circle as @Andrey shows, so something of the form function drawCircle(center, radius). That allows you to keep complexity down.
If your circles can overlap, then the solution is quite easy: look at a circle as an object with a center point and distribute these center points evenly over the square. Don't use rand to do this, but calculate their positions yourself.
If you can't find a solution, I might expand my answer in a few days.

A:

Without diving too deep into your code, I think that you need to add a hold function, after the first plot
 for t = 1: size_x
     plot(x_base_urban(t)+ r_base_urban.*cos(0:2*pi/100:2*pi),...
     y_base_urban(t)+ r_base_urban.*sin(0:2*pi/100:2*pi),'parent',hAxs)
     plot(x_base_urban(t),y_base_urban(t),'+','parent',hAxs)
     hold(hAxs,'on');
 end

By the way, the best way to draw circle is by using rectangle command.
 rectangle('Curvature',[1 1],'Position',[1 3 4 5])

So you can create a PlotCircle function (like @EgonGeerardyn suggests) like this:
 function plotCircle(x,y,r)
      rectangle('Position',[x-r y-r 2*r 2*r],'Curvature',[1 1]);
 end

