2

I have a thousands of variables stores in .mat files. I am loading them in a loop one at a time. I then assign the loaded variable the workspace variable x using eval. Then, I clear the loaded variable. The problem is that matlab gives The current workspace already has too many variables error after around 60,000 iterations. But, on checking the workspace, it appears that only 5-10 variables are present.

for i = 1:m
    load(sprintf('feat_%s.mat', ids{i}), sprintf('feat_%s', ids{i}));
    eval(sprintf('x = feat_%s;', ids{i}));
    clear(sprintf('feat_%s', ids{i}));
end

I think this has got to do with the eval command. I am guessing that eval is creating multiple copies of x. Any idea on wow to clear variables created using eval.

Shai
  • 111,146
  • 38
  • 238
  • 371
stressed_geek
  • 2,118
  • 8
  • 33
  • 45

1 Answers1

1

It is best not to use eval. You can load into a variable:

for ii=1:m
   nm = sprintf('feat_%s', ids{ii} );
   ld = load( [nm,'.mat'], nm );
   x = ld.(nm); % access loaded variable WITHOUT eval
   clear ld; % clear the loaded variable
end

PS,
It is best not to use i as a variable name in Matlab.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371