Hold on and Hold off command keeps all plots on subsequent graphs

조회 수: 18(최근 30일)
Roman
Roman 2022년 11월 28일
편집: VBBV 2022년 11월 28일
Using the hold on and hold off causes the first graph to plot correctly but the second graph keeps the same plots as the first and adds new plots. I have used section breaks and tried clear and clc. I just need the current two plots to be on the same graph and move on to the next two plots on the same graph. How do I get it to stop putting everything on one graph?
syms x
eqn1 = x^2*cos(x)
eqn1 = 
d_eqn1 = diff(eqn1,x)
d_eqn1 = 
hold on
fplot(eqn1,[0,4*pi])
fplot(d_eqn1,[0,4*pi])
hold off
clear
clear
clc
syms x
eqn2 = (x^(5/3))+(9/(sqrt(x)))
eqn2 = 
d_eqn2 = diff(eqn2,x)
d_eqn2 = 
hold on
fplot(eqn2,[-10,10])
fplot(d_eqn2,[-10,10])
hold off
clear

답변(3개)

Jan
Jan 2022년 11월 28일
편집: Jan 2022년 11월 28일
clear clears the variuables of the workspace. clc clears the command window. Both do not have any connection to the state of the current axes object. Do not try to program using wild guessing.
cla clears the current axes.
You have observed the expected behavior:
hold on % Current state is: hold
plot(rand(1, 10));
plot(rand(1, 10));
hold off % Current state is: not hold
... % Do what ever you want except clearing or deleting current aces
hold on % Current state is: hold
plot(rand(1, 10)); % Of course this appears in the former axes
plot(rand(1, 10));
hold off % Current state is: not hold
So if you do not want the existing axes to be re-used, do not call hold('on') before the commands to plot:
plot(rand(1, 10));
hold on % Current state is: hold % After plotting
plot(rand(1, 10));
hold off % Current state is: not hold
... % Do what ever you want except clearing or deleting current aces
cla % For example, or:
plot(rand(1, 10)); % Of course this appears in the former axes
hold on % Current state is: hold, after plotting
plot(rand(1, 10));
hold off % Current state is: not hold

VBBV
VBBV 2022년 11월 28일
편집: VBBV 2022년 11월 28일
figure(1)
fplot(eqn1,[0,4*pi])
hold on
fplot(d_eqn1,[0,4*pi])
figure(2)
fplot(eqn2,[-10,10])
hold on
fplot(d_eqn2,[-10,10])
hold off
  댓글 수: 1
VBBV
VBBV 2022년 11월 28일
편집: VBBV 2022년 11월 28일
Use figure call to plot them separately as above. As @Jan mentioned clear and clc clears the workspace variables and command window output. It has no connection with figure window outputs

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


Jonas
Jonas 2022년 11월 28일
do you want the second part into another axes or also into another figure?
syms x
eqn1 = x^2*cos(x);
d_eqn1 = diff(eqn1,x);
figure;
fplot(eqn1,[0,4*pi])
hold on
fplot(d_eqn1,[0,4*pi])
figure;
eqn2 = (x^(5/3))+(9/(sqrt(x)));
d_eqn2 = diff(eqn2,x);
fplot(eqn2,[-10,10])
hold on
fplot(d_eqn2,[-10,10])
alternatively you could put them into a common figure using tiledlayout or subplot
figure
tiledlayout(2,1);
nexttile
fplot(eqn1,[0,4*pi])
hold on
fplot(d_eqn1,[0,4*pi])
nexttile
fplot(eqn2,[-10,10])
hold on
fplot(d_eqn2,[-10,10])
figure
subplot(2,1,1)
fplot(eqn1,[0,4*pi])
hold on
fplot(d_eqn1,[0,4*pi])
subplot(2,1,2)
fplot(eqn2,[-10,10])
hold on
fplot(d_eqn2,[-10,10])

범주

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

Community Treasure Hunt

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

Start Hunting!

Translated by