I am trying to run the same function multiple times within a script. The function plots a figure after I input the variables. I am trying to superimpose the multiple figures into one graph while using the same function. I used the "hold on" command after each plot, but I only get the graph of the first inputted variables. How do I reiterate the use of the function?

답변 (1개)

Rick Rosson
Rick Rosson 2014년 9월 7일

0 개 추천

You need to create the figure and axes in the script before you call the function that does the plotting. You should pass the axes handle as an input to your potting function.
Your script should look something like the following:
fig = figure;
ax = axes;
hold on
myplotfunc(ax,x,y,z);
myplotfunc(ax,u,v,w);
...
Inside your plotting function, make sure you pass the axes handle as the first argument to any plot command that you call.
HTH.

댓글 수: 3

Farid
Farid 2014년 9월 7일
Maybe if I showed you what I was talking about, then it would be a little more of clear what I am talking about:
Function: function P = plot_arc(a,b,i,j,r,color) % This function plots a circular arc in the cartesian plane. % a is the lower bound of the arc segment in radians. % b is the upper bound of the arc segment in radians. % (i,j) is the center of the circle. % r is the radius of the circle.
t = linspace(a,b); %dictates the resolution of the datapoints between a and b x = r*cos(t) + i; %generates the x component of the arc y = r*sin(t) + j; %generates the y component of the arc
P = fill(x,y,color); %fills the arc with desired color showaxes('hide'); %hides the axes of the graph
Superimposed image I am trying to get: %% plots the white semi-circle on the right a = 0; b = pi; i = 0; j = 0; r = 4; color = 'k';
P = plot_arc(a,b,i,j,r,color); set(P,'edgecolor','k','linewidth',1)
% Plots the black semi-circle on the left
a = pi; b = 2*pi; i = 0; j = 0; r = 4; color = 'w';
P = plot_arc(a,b,i,j,r,color); set(P,'edgecolor','k','linewidth',1)
axis([-7 7 -7 7]) %adjusts axes axis square; %adjusts scale of axes
I just get the first figure.
Rick Rosson
Rick Rosson 2014년 9월 7일
Please format your code so that it is easy for others to read.
Rick Rosson
Rick Rosson 2014년 9월 7일
편집: Rick Rosson 2014년 9월 7일
Please review the answer that I provided and try implementing it. It will work.
Hint:
P = fill(x,y,color,'parent',ax);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

2014년 9월 7일

편집:

2014년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by