"clear all" causing annoying warning messages

14 vues (au cours des 30 derniers jours)
Sarah T
Sarah T le 8 Août 2017
Déplacé(e) : Stephen23 le 10 Avr 2024
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 commentaires
KSSV
KSSV le 8 Août 2017
Are you sure Clear all is placed only at the start of the code?
Stephen23
Stephen23 le 8 Août 2017
Modifié(e) : Stephen23 le 8 Août 2017

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 8 Août 2017
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 commentaire
Sarah T
Sarah T le 8 Août 2017
Thank-you. I have replaced it with "clearvars" and this seems to work fine.

Connectez-vous pour commenter.

Plus de réponses (1)

David Cazenave
David Cazenave le 9 Avr 2024
Déplacé(e) : Stephen23 le 10 Avr 2024
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.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by