"clear all" causing annoying warning messages
조회 수: 13 (최근 30일)
이전 댓글 표시
I am using "clear all" at the start of a ".m" file that I have written myself (lets call it "myfile.m"). When I run the file, I get three identical messages stating "Warning: The file 'myfile.m' could not be cleared because it contains MATLAB code that is currently executing." I don't want to turn warnings off, because I am writing my own warnings later in the file. Why does this happen and how can I prevent it? I am using Matlab version 2017a.
댓글 수: 2
Stephen23
2017년 8월 8일
편집: Stephen23
2017년 8월 8일
"clear all" causing annoying warning messages"
Putting clear all at the top of every script is one of the most common signs of cargo-cult programming in MATLAB. The best solution is: do not put clear all at the top of every mfile, and learn to write functions. Problem solved!
Search this forum to know more about why putting clear all everywhere is a bad practice (as you are starting to find out):
etc, etc, etc.
채택된 답변
Jan
2017년 8월 8일
clear all clears Matlab's memory in a brutal way. While removing all locally used variables might be useful for scripts, which are short hacks or prooves of concept, it is evil is productive code. Prefer clean workspaces by using functions instead.
Then clear all deletes all functions from the RAM, and the reloading from the slow hard disk wastes a lot of time. Doing this frequently means a massive loss of speed. In your case Matlab warns, that some functions cannot be removed. Perhaps they are callbacks of GUIs or timers, or the currently processed M-code.
In addition all persistent variables are cleared and many functions need a time-consuming re-initialization afterwards.
In older Matlab versions clear all has deleted all debugger breakpoints also, and anything, which impedes debugging is an enemy of teh programmer.
The solution is trivial: Omit the evil clear all. Instead of hiding the warning message, this is much more efficient.
추가 답변 (1개)
David Cazenave
2024년 4월 9일
이동: Stephen23
2024년 4월 10일
You can still turn off the warnings, and then turn them back on before your 'custom warnings.' If that helps, Thank you ... Thank you very much.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!