Plotting noncontinuous arc segments

6 views (last 30 days)
Hey all,
I am trying to plot a non-continuous arc of a circle, given the center, radius and angle ranges of the arc(s). For instance, say I want to plot the following angle range:
0,...,0.75*pi,1.25*pi,...,2*pi
There's a gap between 0.75*pi and 1.25*pi. I don't want MATLAB to plot anything "between" these two angles, but it plots a straight vertical line.
This the basic structure of my code:
t=[];
for theta=0:0.01:2*pi
if % irrelevant condition that decides if the arc covers this angle
t=[t theta];
end
end
plot(a1*cos(t),a1*sin(t),'b--')
And here is the figure:
I want to 'skip' the vertical line. Is this possible?
Any kind of assistance would be greatly appreciated!

Accepted Answer

John D'Errico
John D'Errico on 15 Apr 2015
Edited: John D'Errico on 15 Apr 2015
You ARE plotting 0 to 2*pi. That is 360 degrees. You have an x,y, pair at EVERY one of those points. Then you pass those numbers to plot. How should it know that even though you told it to plot those points, that you don't really mean it?
You CAN buy the mind reading toolbox when it comes out, but I hear that it will be incredibly expensive.
Better is to not create values for t for those points. Simpler is to fill t with NaNs when that test fails.
Finally, BEST is to preallocate your array in advance. This will be seriously faster. And one day if you never learn to preallocate your vectors and arrays in cases like this, you will come back to this forum, and ask why is your code SOOOOO slow!
theta = 0:0.01:2*pi;
t = theta;
for ind = 1:numel(theta)
if % irrelevant condition that decides if the arc does NOT cover this angle
t(ind) = NaN;
end
end
plot(a1*cos(t),a1*sin(t),'b--')
Better yet would be to build t in a vectorized way, but since you have told us nothing more than that the test is irrelevant, I can't help you there. It might be something as simple as this:
t = linspace(-3*pi/4,3*pi/4,100);
a1 = 2;
plot(a1*cos(t),a1*sin(t),'b--')
axis equal
Learn to write code that uses the capabilities of MATLAB in an efficient way.
  1 Comment
Guy Lalkin
Guy Lalkin on 16 Apr 2015
Thanks for the answer! I learned a lot. I guess it comes down to understanding what the 'plot' command does. Anyway, thanks for the rundown!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!