What do you think of "Did you mean clearvars -except".....?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I often use clearvars -except, e.g.
clearvars -except temperature loads speeds
but because I don't sub-vocalize the hyphen I too often accidentally type
clearvars except temperature loads speeds
and then I have the unfortunate result of deleting the very same variables I meant to retain!
My proposal is that when one doesn't have a variable "except", the "Did you mean" feature should kick in with
Did you mean clearvars -except temperature loads speeds
Anyone else had this? What do you think of this suggestion?
3 commentaires
Réponse acceptée
Philip Borghesani
le 14 Sep 2016
The suggestion is interesting but not possible to implement. "did you mean?" is implemented with a try-catch block but your clearvars example never throws an error. The only possible implementation would be to modify the clearvars function to have a special case for the variable "except" which would be an incompatibility.
Plus de réponses (1)
John D'Errico
le 13 Sep 2016
Modifié(e) : John D'Errico
le 13 Sep 2016
Thats funny. I very rarely find the need to use clear/clearvars at all. I know what lives in my workspace. And I run MATLAB continuously, virtually never exiting unless I reboot my computer. And given that I am constantly playing with different problems to answer questions on Answers, testing some new code of mine, etc., there are lots of things that live in my current workspace. (A fair amount of deadwood too.)
The point is, if you will use a variable x (or whatever variable) then you either just created x to be used, or you know what is in x.
If you use scripts a lot, that will access variables blindly from your main workspace, then learn to avoid scripts. Learn to write and use functions. Scripts are a tool that novices learn early in their careers, but cause them an infinite number of problems as they advance in MATLAB.
The way I see it, IFF you know what lives in your workspace well enough that you confidently know what it is you need to keep, then deleting the rest is a waste of time. If you don't know for sure what you are deleting, or are sufficiently sloppy in your typing so that you forget the - in front of except, then clearing variables en-mass is dangerous.
So personally, I see this as an unnecessary crutch. But I can accept that some people use, and even like crutches.
Ok, so IF you feel that you need the crutch, then use a tool like keep (found on the file exchange) or one of the many variations posted there over the years.
By forcing you to use a different tool when you want to delete all BUT some list, it completely avoids the problem. So use clear or clearvars when you want to explicitly clear some list. When you want to delete all BUT some list, then use keep.
5 commentaires
John D'Errico
le 13 Sep 2016
Modifié(e) : John D'Errico
le 13 Sep 2016
oops. yes. It needs to use an evalin to accomplish the clear. Still pretty trivial to write. Even better, just download the original.
Walter Roberson
le 13 Sep 2016
I very rarely find the need to use clear/clearvars at all
I use clear sometimes. I do a fair bit of "one-off" coding, small loops, to find solutions to things the users have posted, with it not being worth my time to create a directory and cd to it and write functions there just to keep the workspace clean.
I also take user code and paste it in to my command window, and the behavior of that user code can depend upon which variables already exist. For example one user might have defined a variable i and the next one's problem might be that they did not assign to i and are getting the function i() instead, so before running the second user's code I need to clear the state so that I am accurately replicating the state the user would have.
There is also the factor that the Symbolic Toolbox remembers state by itself, including some temporarily evaluation results -- if you have done an integral once then it might remember the result instead of re-calculating it, but you might need to recalculate it. That and the fact that the Symbolic Toolbox can be a memory hog.
Also, when you are constructing parallel code, sometimes parfor needs the hint provided by a "clear" of a specific variable in order to be sure that you are using the variable as a temporary, especially if you have used the same variable name before the parfor.
When working with datasets that are a substantial fraction of the available memory, sometimes it is advisable to "clear" an array when you are finished with it, especially if the next step involves a calculation that needs more temporary storage than final storage, as it might be necessary to have as much free space available as you can muster in order for the operation to finish at all. "Just throw hardware at it" does not work if you have maxed out the hardware or the hardware costs exceed the available budget.
I never use "clearvars" or "keep" though.
Voir également
Catégories
En savoir plus sur Performance and Memory dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!