Error using eval when load many files in recursive

I want to load many files, i.e.
load('SST_mw_region6.mat','sst','time','lon','lat');
where "6" can be vary from 1 to 20
I did try using :
for k=1:20
eval(['load SST_mw_region' num2str(k) '.mat','sst','time','lon','lat';'])
end
I am not sure where I have to put ' ' and how many of them
Thanks
Dwi

답변 (1개)

Walter Roberson
Walter Roberson 2019년 9월 20일

2 개 추천

Don't use eval(). Construct your filenames using sprintf() and use the function form of load instead of the command form.
Note that when you do not assign the output of load() to a variable, that each iteration each of the variables you just loaded will be overwritten completely with the variables you just loaded.

댓글 수: 4

DwiNY
DwiNY 2019년 9월 20일
Walter,
Thank you for the suggestions. Yes, I am assigning output load to a variable (which I do not include in there). I am looking for answer how to write the eval load command as above, with the right " ' . It seems I missed some of ".
Thanks
Dwi
Stephen23
Stephen23 2019년 9월 20일
편집: Stephen23 2019년 9월 20일
" I am looking for answer how to write the eval load command as above..."
Sure, that was clear from your question. What Walter Roberson explained is that you do NOT need to use eval, you can use the simpler and more efficient function syntax, e.g.:
F = sprintf('SST_mw_region%d.mat',k);
S = load(F,'sst','time','lon','lat');
See also:
Your approach using eval is a red-herring, and serves only to distract you from the simpler solution and also from your real question (which is "how can I call load with different filenames in a loop" or something similar, your question is a good example of the http://xyproblem.info/ ).
Also note that your question has nothing to do with recursion.
As Walter said, don't use eval. With eval you're effectively writing code that generates code. (one of) The (many) problem(s) is you can't easily tell what code is being generated and thus can't easily debug it. E.g. in your case, you're "not sure where I have to put ' ' and how many of them". You're giving yourself twice as much work to do. You have to make sure that your code generator is correct (the eval line) and that the generated code is correct (the stuff eval creates).
There are plenty of ways to construct file names (as per Walter's link). In your case, this should work:
numfiles = 20;
matdata = cell(1, numfiles); %preallocate cell array that stores the data from each matfile
for file = 1:numfiles
matdata{file} = load(sprintf(' SST_mw_region%d.mat', file), 'sst', 'time', 'long', 'lat');
end
%optional: since all cells will be scalar structures with the same fields
matdata = [matdata{:}]; %matdata will be a 1xnumfiles structure array.
DwiNY
DwiNY 2019년 9월 20일
Dear Walter, Stephen and Guillaume,
Thank you so much for your kind help
Dwi

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2019년 9월 20일

편집:

2019년 9월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by