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

449 views (last 30 days)
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

Answers (3)

Jan
Jan on 28 Nov 2022
Edited: Jan on 28 Nov 2022
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 on 28 Nov 2022
Edited: VBBV on 28 Nov 2022
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 Comment
VBBV
VBBV on 28 Nov 2022
Edited: VBBV on 28 Nov 2022
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

Sign in to comment.


Jonas
Jonas on 28 Nov 2022
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])

Categories

Find more on Specifying Target for Graphics Output 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!