File Exchange Pick of the Week

March 21st, 2008

Drawing a Circle

You may have noticed some recent changes in the format of this blog. Here’s what to expect on a regular basis – two topics per week.

  • On Tuesdays Doug will provide MATLAB tutorials.
  • On Fridays guest bloggers Jiro, Brett and Bob will highlight File Exchange submissions.

In last week's Pick of the Week, I talked about fitting a circle to a collection of points. With a small example, I showed how Izhak Bucher's CircFit makes short work of that task. But to visualize the circle I fit through my faked data, I used MATLAB's rectangle command! If that seems odd to you, then this week's pick is for you: Zhenhai Wang's Draw a Circle submission lets you visualize your circle with (to me) a much more natural syntax, specifying the coordinates of the center, the radius of the circle, and the number of points to plot. Optionally, the file also lets you specify the style parameters for the plotted curve.

For instance:

R = 10;
Center = [5,8];
circle(Center,R,1000,'b-');
hold on
plot(Center(1),Center(2),'g.')

Okay, so you may be thinking that plotting a circle with this syntax isn't the most difficult thing you've ever tried to do. Still, nearly 24,000 people have downloaded Zhenhai's file! This suggests that it might not be trivially easy, either. Or at least, that it's awfully convenient to be just be the beneficiary of someone else's work. And "Draw a Circle" is pretty well vetted, too; it has an average of 4.29 (out of 5) stars, from 42 reviews. That means a lot of people are happy with Zhenhai's effort. In fact, years after its submission, "Draw a Circle" continues to get hundreds of downloads every month (661 in the past 30 days). Moreover, two other files cite this week's selection as an inspiration. Nice work, Zhenhai!

Got a favorite plotting routine that makes your life easier? Let us know about it!


Get the MATLAB code

Published with MATLAB® 7.6

8 Responses to “Drawing a Circle”

  1. Urs (us) Schwarz replied on :

    if only this dear author had replaced the PLOT with a LINE command - or - at least used the possibilities of HOLD-ing…
    us

  2. Terry replied on :

    Complex coordinates are easier: Eg.
    R = 10;
    Center = [5,8];
    plot(R/2*exp(i*linspace(0,2*pi,1000))+Center*[1 i].’,'b-’)
    axis square

  3. sunpeng replied on :

    hi Doug,

    Why not have a look at “imPixelLine and imCircle” in FileExchange…

    Do the same job, with more functionalities.

  4. Brett replied on :

    Sometimes we may focus on a submission because it is beautifully written…other times, simply because it does something useful, and does it well. Zhenhai’s “Circle” certainly fits that latter bill.

    Urs, why would you necessarily think that HOLD-ing should be incorporated in the CIRCLE command? Many useful functions use PLOT commands–and in all cases it is up to the user to issue (or not, –depending on his or her desired behavior) the HOLD command.

    Terry, I’m not sure there’s anything inherently easier about using complex coordinates than the very compact pol2cart that this function uses.

    Sunpeng, I haven’t really played with your code, and it certainly appears that you’ve added some nice functionality. But I will note that your file requires the Image Processing Toolbox. I could argue that generic functionality like plotting a circle should be independent of that (or any) Toolbox.

  5. Ralf Hielscher replied on :

    Does nobody knows the builtin MATLAB command

    rectangle(’Position’,[x,y,dx,dy],’Curvature’,[1,1])

    which allows for all the builtin MATLAB options including hold? The main advantage of which is to do not use any discretization.

  6. Brett replied on :

    Ralf, of course we know (and love) the RECTANGLE command. (Follow the link in this post to “last week’s Pick of the Week.”) But options are always nice.

  7. Jose Manuel replied on :

    Hello there

    I need a matlab function to paint and store in an array all the poits INSIDE the circle.

    Please, anybody could help me?

    Thanks in advance

  8. Brett replied on :

    Jose,
    You might try representing your circle(s) as polygons, then using the MATLAB function INPOLYGON. Just a thought.
    Cheers,
    Brett

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.