Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear variables from workspace with exceptions by use of regular expressions

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.

like image 630
Robert Seifert Avatar asked Nov 19 '13 13:11

Robert Seifert


People also ask

How do you clear workspace variables?

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 .

How do you clear all variables except in Matlab?

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 .

Which command is used to remove variables from memory?

The drop command is used to remove variables or observations from the dataset in memory.

How do you clear values from the command window workspace and command history?

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.


1 Answers

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).

like image 117
Rody Oldenhuis Avatar answered Oct 17 '22 02:10

Rody Oldenhuis