How can I use subplots with an inside function?

I am trying to create a figure with 6 subplots. My 'polygon' function creates a regular plot. The subplot command doesn't seem to recognize the function as a plot, and only the last plot is displayed (as a full plot). What am I missing? Thanks!
function draw_polygons()
subplot(2,3,1)
polygon(3);
subplot(2,3,2)
polygon(4);
subplot(2,3,3)
polygon(5);
subplot(2,3,4)
polygon(6);
subplot(2,3,5)
polygon(7);
subplot(2,3,6)
polygon(8);
end

5 件のコメント

Walter Roberson
Walter Roberson 2013 年 3 月 8 日
Please show us the code for polygon()
gm76
gm76 2013 年 3 月 8 日
function polygon(sides)
degrees=2*pi/sides;
theta=0:degrees:2*pi;
radius=ones(1,numel(theta));
graph=polar(theta,radius);
set(graph,'color','b','linewidth',2)
end
gm76
gm76 2013 年 3 月 8 日
Any ideas?
Image Analyst
Image Analyst 2013 年 3 月 8 日
It worked fine for me. All 6 plots showed up.
Daniel Shub
Daniel Shub 2013 年 3 月 8 日
What happens if you just do
subplot(2,3,1)
subplot(2,3,2)
subplot(2,3,3)
subplot(2,3,4)
subplot(2,3,5)
subplot(2,3,6)

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2013 年 3 月 8 日

0 投票

Are you saying that this code does not produce 6 plots? Because it does for me (both functions are in test.m):
function test
subplot(2,3,1)
polygon(3);
subplot(2,3,2)
polygon(4);
subplot(2,3,3)
polygon(5);
subplot(2,3,4)
polygon(6);
subplot(2,3,5)
polygon(7);
subplot(2,3,6)
polygon(8);
end
function polygon(sides)
degrees=2*pi/sides;
theta=0:degrees:2*pi;
radius=ones(1,numel(theta));
graph=polar(theta,radius);
set(graph,'color','b','linewidth',2)
end

3 件のコメント

gm76
gm76 2013 年 3 月 8 日
I only get a plot with polygon(8) which is an octagon. There are no subplots. I have each function in a separate m-file.
gm76
gm76 2013 年 3 月 8 日
I tried putting the code for polygon in the same m-file like you did and it works. I don't understand why it doesn't work if they're in separate files.
Image Analyst
Image Analyst 2013 年 3 月 8 日
編集済み: Image Analyst 2013 年 3 月 8 日
It should work in a single file, or two different m-files. You must have a close() or clf in there somewhere.

サインインしてコメントする。

Leah
Leah 2013 年 3 月 8 日

0 投票

make sure you don't have a close or figure command inside of polygon
you could always try to step through with debugger to find out exactly what is happening in your code

1 件のコメント

gm76
gm76 2013 年 3 月 8 日
Yeah I had a clf command inside polygon. Works perfectly now. Thanks!

サインインしてコメントする。

Jake_K
Jake_K 2018 年 4 月 30 日

0 投票

I guess your problem is that you always open a new figure in your polygon function. So take out figure() and it should work.

質問済み:

2013 年 3 月 8 日

回答済み:

2018 年 4 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by