Scipio
Scipio

Reputation: 313

MATLAB - "clear -except a" only deletes a

Everywhere I look it says that the expression clear -except a should clear all variables except for a. However, if I try it (MATLAB R2014a, running on Linux Mint 17 Cinnamon 64-bit), matlab clears only the variable a ("-except a" is shown in purple, apparently being processed as a string). I tried clear('-except a'), which does not do anything, and 'clear - except a' - with a space between "-" and "except", which results in an error (too many arguments).

What am I doing wrong?

[edit]

Here are the full errors I obtain for clearvars:

EDU>> a = 5;
EDU>> clearvars -except a;
Error using strjoin (line 14)
Expected input to be one of these types:

char

Instead its type was cell.

Error in clearvars>createPattern (line 187)
    pattern = sprintf('(%s)$', strjoin(variableArgs, '|'));

Error in clearvars (line 76)
    exceptVarPat = sprintf('(?!%s)', createPattern(exceptVarArgs, flags.regexpExcept));

Upvotes: 1

Views: 3983

Answers (2)

Ufos
Ufos

Reputation: 3305

Or alternatively you can use regular expressions for that. For instance, if you have variables containing the word "path", you can clear all, excluding those by calling:

clear -regexp ^((?!path).)*$; % do not clear paths

Upvotes: 1

Scipio
Scipio

Reputation: 313

The problem occurred because of a file named "strjoin.m" in the PropertyGrid package (that I had as a dependency of BCILab, an external package inside SIFT, a plugin for EEGLAB). Because of the function precedence order in MATLAB, this function was called instead of the function located in matlab/toolbox/strfun/strjoin.m. Renaming the plugin file solved the problem.

(Although I am not sure if this is the best solution, as it brings up the possibility for future bugs when PropertyGrid calls strjoin. In this case I think I will be fine, but comments on the best approach in similar situations would be appreciated).

Upvotes: 2

Related Questions