How to clear variables created using eval?
13 views (last 30 days)
Show older comments
I need to use EVAL to calculate a formula in a loop, the formula and the variables involved are just unknown in advance. Then I clear the loaded variables. The problem is that matlab gives 'The current workspace already has too many variables' error after several iterations. But, on checking the workspace, it appears that less than 50 variables are present. I think this has got to do with the eval command. I am guessing that eval is creating multiple copies of 'Vlu' and 'Idx'. I need an idea on how to clear variables created by eval. Before running the program, you need to create a file named as Formula.txt to save formula-strings, one formula lays on one line, such as:
[Vlu, Idx] = max([C345, C123 + C765, C999 * 2]);
[Vlu, Idx] = min([C235, C137 - C222, C259 / 2]);
...
and all vars such as 'C345' have saved in Vars.mat in advance. It should be understood that the following code is just a model while the actual code is much more complex, which has more iterations.
% Read formulas from TXT file, one formula lays on one line
fid = fopen('D:\Formula.txt');
k = 0;
f = fgetl(fid);
while ischar(f)
k = k + 1;
Formula{k} = f;
f = fgetl(fid);
end
fclose(fid);
Rst = zeros(length(Formula), 2); % Result-buffer
for m = 1:length(Formula)
Vars = regexp(Formula{m}, '[C][0-9]\d*', 'match');
% Getting Var-names in a formula
load('D:\Vars.mat', Vars{:}); % Load vars from .mat
eval(Formula{m}); % Error would occur here!
Rst(m, :) = [Vlu, Idx];
for k = 1:length(Vars) % Clear Vars after calculating
eval(['clear(''', Vars{k}, ''')']);
end
end
6 Comments
Stephen23
on 6 Aug 2015
Edited: Stephen23
on 6 Aug 2015
It seems that the whole concept should be revised, given that the .mat file contains hundreds of numbered variables and there a an unknown number of formula. How were these variables created? How large are they? How are the formula defined?
The most efficient solution depends on what kind of variables they are, and how they are generated, but I would suggest that instead of creating hundreds of numbered variables simply create some arrays and use indexing. Accessing arrays is fast and easy, and does not cause this kind of problem.
Actually what you are trying to do is to run arbitrary functions on arbitrary data. This is not going to be very efficient use of MATLAB, nor is it going to be robust, nor easy to debug, as you discovered. Every time you use eval you miss out on lots of useful inbuilt tolls that help the code work properly, and give advice on how to make your code faster and more efficient.
Why not just write the formula as an actual MATLAB script and keep the data in arrays. You can easily alter the formula using function handles, and access the data quickly using indexing.
Read this to know more about why you should avoid eval:
Walter Roberson
on 7 Aug 2015
Have the file extension be .m . run() the file, or just name it to call it as a script.
Note: if you are using "function" with matching "end", then the script you execute will not be permitted to create new variables -- but it would be permitted to create fields on existing variables. It would also be fine to initialize each of the permitted variables to something (anything, including []) before executing the script.
Answers (2)
Walter Roberson
on 6 Aug 2015
Use fields of a structure. Structures can add fields at need. An appropriate structure is the return value from load()
VS = load('D:\Vars.mat', Vars{:});
Now you can refer to VS.(Vars{K}) if you need to work with the K'th variable, or you can refer by absolute name like VS.apogee3H
2 Comments
Stephen23
on 6 Aug 2015
Edited: Stephen23
on 6 Aug 2015
How are the formula created?
How are the hundreds of numbered variables in the .mat file created?
Look at what you are trying to do: execute each row of Formula.txt with some data. So if Formula.txt contains executable code, why not simply turn it into a script and run that? What is the point in this incredibly inefficient indirect execution of perfectly good executable code?
Image Analyst
on 6 Aug 2015
Why don't you just do this:
clear all;
? If there are a bazillion named variables, are there any that you want to keep? If so, you can't use clear all. But if you want to just blow all of them away, use clear all.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!