How to create a polar rectangle in Matlab

Please help me determine how to create a polar rectangle (not sure what to call it) in Matlab. The closest I have gotten is using the linspace function for the two arc and a simple line to connect the two arcs. I can't seem to cleanly connect the four line segments to make a continuous shape.

 Accepted Answer

I am not certain what you want.
Try this:
theta = linspace(0, 2*pi, 5);
figure(1)
polarplot(theta, ones(size(theta)))

8 Comments

Thanks for the quick response. I see that my hand drawn plot didn't upload. Please see picture to explain further.
My pleasure.
It didn’t, so I was left wondering.
Try this:
theta = linspace(3*pi/4, 5*pi/4, 25);
arcrad = [0.25; 0.75]; % Arc Radius Vector
figure(1)
polarplot(theta, [arcrad(1)*ones(size(theta)); arcrad(2)*ones(size(theta))], '-k', 'LineWidth',2)
hold on
polarplot([theta([1 end]); theta([1 end])], [arcrad(1)*[1 1]; arcrad(2)*[1 1]], '-k', 'LineWidth',2)
polarplot([theta(1)*[1 1]; theta(1)*[1 1]], [[0 0]; arcrad(1)*[1 1]], ':k');
polarplot([theta(end)*[1 1]; theta(end)*[1 1]], [[0 0]; arcrad(1)*[1 1]], ':k');
hold off
The first two polarplot calls plot the ‘rectangle’. The last two polarplot calls plot the dashed/dotted lines from the origin to the inner radius.
Experiment to get different orientations and other variations.
Ron Beck’s Answer moved here:
Thank you very much. That is exactly what I was looking for.
As always, my pleasure.
If my Answer helped you solve your problem, please Accept it!
One more question, what would be the code to create a circle on the same plot?
Add this line between hold on and hold off calls to draw a circle at a radius of 0.5:
polarplot(linspace(0,2*pi,360), 0.5*ones(1,360), '-r')
I chose 360 points. Experiment with fewer points to get the result you want.
As always, my pleasure.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!