MATLAB Answers

Julian
0

What do you think of "Did you mean clearvars -except".....?

Asked by Julian
on 13 Sep 2016
Latest activity Commented on by Julian
on 15 Sep 2016
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 Comments

"I often use clearvars"
Why?
Clearing variables is slow. And almost totally unnecessary inside functions.
I typically use clearvars at the command line when working interactively, not in a function.. I reckon clear gets used quite a lot by MATLAB users.
Also the "Did you mean" only comes up in command window - not when you writing a function.

Sign in to comment.

2 Answers

Answer by Philip Borghesani on 14 Sep 2016
 Accepted Answer

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.

  1 Comment

Surprised by the "philosophical" nature of the answers to my suggestion, I looked at clearvars and saw it's only an M-function calling jacket for the builtin clear. I guess "did you mean?" is probably baked into MATLAB and responds to invalid command line syntax, whereas my problem use-case is syntactically valid. So I had just about come to the same conclusion, and accept your answer, Thanks.
In comparison with clear, clearvars is pretty recent (from 2007 on my MATLAB). At that point the option flag -except was not added to clear directly. I wonder how often MATLAB users use clearvars; I speculate that clear is far more popular! I am now encouraged to take John D'Errico's suggestion and add keep... Typing clearvars -except is long-winded and dangerous!
Of course, you can achieve same with the workspace browser, but you need to select all, then locate and Ctrl+click each variable you are keeping, before delete - so still quite slow. An invert selection would be helpful in this context.
Typically I might use clearvars -except at the start of a code-section (cell) in the editor, just to confirm the input-output dependencies of a code fragment that will end up in a function.

Sign in to comment.


Answer by John D'Errico
on 13 Sep 2016
Edited by John D'Errico
on 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 Comments

John, the keep function you posted is not going to change anything in the caller's workspace.
oops. yes. It needs to use an evalin to accomplish the clear. Still pretty trivial to write. Even better, just download the original.
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.

Sign in to comment.