I'd like to clear all variables from workspace, but with some exceptions defined by regular expressions.
The function clear
has an additional option -regexp
clear -regexp expr1 ... exprN
clears all variables that match any of the regular expressions listed. This option only clears variables.
So I'm looking for the opposite.
Also there is
clearvars -regexp p1 p2 ...
clears all variables that match regular expression patterns p1, p2, and so on.
clearvars -except v1 v2 ...
clears all variables except for those specified following the -except flag.
Which is already quite nice for complete variable names, but not working for regexp like the first option.
There are solutions on FEX, but I don't want to use additional custom functions.
But As there are such convenient solutions for the just slightly different cases above, I wonder if there is also a simple way to do:
keep -regexp expr1 ... exprN
with inbuilt functions.
To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global . To clear a particular class, use clear myClass . To clear a particular function or script, use clear functionName .
clearvars -except keepVariables removes all variables, except for those specified by keepVariables . Use this syntax to keep specific variables and remove all others. clearvars variables -except keepVariables removes the variables specified by variables , and does not remove the variables specified by keepVariables .
The drop command is used to remove variables or observations from the dataset in memory.
You can use the . cls (Clear Screen) command to clear all of the text from the Debugger Command window. This command clears the whole command history.
Take a look at this for inverse regexp matching. In this context:
clear -regexp ^((?!expr1|expr2|...).)*$
clearvars
is not built-in, but an m-function, which has its own disadvantages. With the inverse regexp matching, you can do everything with clear
(a built-in).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With