Why Does "clear all" Impact Prior Commands in a Script?
Ältere Kommentare anzeigen
Here is the example code. If I comment the "clear all" line in Section 2 the ouput of fplot() in Section 1 comes out fine with no warnings. It's almost like the execution does all the computations first, then goes back to the beginning to generate the plots, which for some reason it can't do correctly in Section 1. I've seen this in live scripts and and m-scripts. Is this expected behavior? If so, it's a bit disconcerting that outputs from commands in Section 1 are influenced by executon of commands in Section 2.
FWIW, I was using "clear all" in Section 2 to make sure that I cleared out all of the symbolic math assumptions from Section 1. But I think I've now found a better (correct?) way to do that, so this issue isn't holding me up at present.
% Section 1
clear all %#ok
syms t real
s(t) = piecewise(t<-1,0, t>2,0, exp(-abs(t)));
figure;
fplot(s(t),[-3 3],'MeshDensity',50)
% Section 2
clear all %#ok
syms t real
s(t) = exp(-abs(t))*rectangularPulse(-2,2,t);
figure;
fplot(s(t),[-15 15],'MeshDensity',50)
3 Kommentare
Sargondjani
am 14 Nov. 2021
Did you read the documentation on "clear all"? I mean, I agree it is weird, but best way is probably to avoid clear all, as recommended in the documentation.
Paul
am 14 Nov. 2021
Rik
am 15 Nov. 2021
I think I would classify this at least as unexpected behavior, if not outright as a bug. Did you contact support already?
The mere fact there are better alternatives (as I expected) doesn't change that.
Akzeptierte Antwort
Weitere Antworten (1)
Sulaymon Eshkabilov
am 15 Nov. 2021
0 Stimmen
In such cases, it is best to use: clearvars instead of clear all
1 Kommentar
Paul
am 15 Nov. 2021
Kategorien
Mehr zu Whos finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

