MATLAB Answers

How do I load all variables from a MATLAB file except a few specific variables?

79 views (last 30 days)
When using LOAD, I would like to load all the variables present in the MATLAB file, except for a few specific ones which I do not want to load. Also, when using CLEAR, I would like to be able to clear all variables in the workspace except a few, specific ones which I want to keep.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Nov 2009
In each case, regular expressions can be used to operate on all variables except for a short list.
You can use LOAD with the -REGEXP option to specify a regular expression. Suppose 'mydata.mat' contains the following variables:
myvar1
variable2
var
a
b
abcd
myvar_abc
data
The following code illustrates several examples:
%%Load all variables except 'var'
load mydata -regexp ^(?!var$).
%%Load all variables except 'a' and 'b'
load mydata -regexp ^(?!a$|b$).
%%Load all variables except 'myvar1', 'myvar_abc', and 'data'
load mydata -regexp ^(?!myvar1$|myvar_abc$|data$).
This idea can be extended to clear all variables from MATLAB's workspace except for a few you do not want to clear. For example, if we have the following variables stored in the workspace:
The = 1;
seed = 2;
of = 3;
the = 4;
tree = 5;
seeds = 25;
aseed = 15;
We can clear all variables except the one named 'seed' by executing the following command:
clear -regexp ^(?!seed$).
Alternatively, we can use the following syntax to clear all variables except for 'seed', 'of', and 'the':
clear -regexp ^(?!seed$|of$|the$).
As of MATLAB 7.6 (R2008a), you can use CLEARVARS with the "-except" flag. For example, the expression
clearvars -except A B
will remove every variable from your workspace except the variables "A" and "B".
You may also use the MATLAB Central file called "keep.m" which provides similar functionality:
<http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=181>
Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.
  2 Comments
Christoph
Christoph on 27 Mar 2015
The difference here is that the above example uses a command form syntax, whereas you used functional syntax. This is explained on the load doc page:
>>doc load
"Do not use command form when any of the inputs, such as filename, are variables."
The above commands do work IF you include the dot at the end:
load mydata -regexp ^(?!var$).
The dot at the end of the above line is part of the load command and not signalling the end of a sentence. Regarding the /w and $: You can look this up on the regexp doc page:
>>doc regexp

Sign in to comment.

More Answers (2)

Toby
Toby on 27 Sep 2017
Edited: Toby on 12 Oct 2017
As another example, you can load all variables except those which already existed in the workspace:
varlist = who; %Find the variables that already exist
varlist =strjoin(varlist','$|'); %Join into string, separating vars by '|'
load(TSfile,'-regexp', ['^(?!' varlist ')\w']);

Joshua Olorocisimo
Joshua Olorocisimo on 4 Feb 2021
for i = 1:number_of_files % number_of_files = # of files you want to go through
load (mat_file{i}, '-regexp', '^(?!a|b$)\w') % load all variables EXCEPT a and b
end
I used this code to load mat files in a for loop without loading certain variables.
It seems the ' \w ' is important which was not in the code of the accepted answer .
I'm using MATLAB 2020b.
  1 Comment
Walter Roberson
Walter Roberson on 4 Feb 2021
If you examine the accepted solution, look carefully and notice that they end in period. That period is part of the code. In regular expressions, period mmatches any one character (including characters such as ç or π or é). \w matches any one "word building" character, which are the upper and lower case English letters, the digits 0 through 9, and the underscore, which is more restrictive.
In context, variable names in a valid mat file will be valid matlab identifiers, which are restricted to be [A-Za-z]\w{0,63} . So using \w will not reject any valid variable name and it does not hurt to use \w . The difference would come in when dealing with files that were created outside of MATLAB with variable names that are not valid matlab identifiers, as it has not been possible to load such variables for about the last decade. If, hypothetically, you were dealing with such files then \w would be too permissive and [A-Za-z] should be used instead.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!