Hold on and Hold off command keeps all plots on subsequent graphs
조회 수: 449 (최근 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)
d_eqn1 = diff(eqn1,x)
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)))
d_eqn2 = diff(eqn2,x)
hold on
fplot(eqn2,[-10,10])
fplot(d_eqn2,[-10,10])
hold off
clear
댓글 수: 0
답변 (3개)
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
댓글 수: 0
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])
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!