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

447 次查看(过去 30 天)
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])

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by