Access variables in several .mat files

조회 수: 5 (최근 30일)
Isma_gp
Isma_gp 2016년 9월 8일
편집: Stephen23 2017년 4월 12일
Hi,
I have 4 .mat files, each one with the same variables. I want to access some of those variables for each of the files.
The variables I want to access are called 'RW_01', 'RW_02',....., 'RW_20' in each of the files, with size 1x160000.
The result should be a 4(files)x 20(variables) cell. Each one containing the information for that specific file and variable i.e. 1x160000
Can I get some help? Thanks

채택된 답변

Stephen23
Stephen23 2016년 9월 10일
편집: Stephen23 2017년 4월 12일
Here is a simpler, faster method, without using slow and buggy eval:
files = {'file1.mat', 'file2.mat', 'file3.mat', 'file4.mat'};
S = load(files{1});
for m = 2:numel(files)
S(m,1) = load(files{m});
end
C = struct2cell(S)';
and it gives exactly the cell array requested in the original question:
>> size(C)
ans =
4 20
>> size(C{1,1})
ans =
1 160000
This code assumes that the only fields in the mat files are those that are required. If there are other fields in the mat files, simply specify the required fields in the load call:
names = arrayfun(@(n)sprintf('RW_%02d',n),1:20,'Uni',false);
S = load(files{1},names{:});
etc.
Tip for beginners: rather than fighting MATLAB by using eval and other buggy, slow, and obfuscated commands, learn how to use MATLAB effectively by understanding matrices, arrays, and indexing:
A discussion of why eval is not the solution that beginners imagine it to be:
  댓글 수: 1
Isma_gp
Isma_gp 2016년 9월 13일
Thank you! very interesting.

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

추가 답변 (2개)

Thorsten
Thorsten 2016년 9월 8일
편집: Thorsten 2016년 9월 9일
matfile = {'mymatfile1.mat', 'mymatfile2.mat', 'mymatfile3.mat', 'mymatfile4.mat'};
RW(16000,20,4) = 0; % preallocate for speed
for i =1:numel(mayflies)
load(matfile{i}); % or load(sprintf('mymatfile%d', i));
for j = 1:20
eval(['RW(:,j,i) = RW_' sprintf('%02d',i) ';']); end
end
end
  댓글 수: 3
Thorsten
Thorsten 2016년 9월 8일
편집: Thorsten 2016년 9월 8일
Nice alternative. So why don't you post a complete example? Maybe because it is a bit more complicated and less straight forward than my solution?
After all, eval is a valid Matlab command and has not yet been replaced by the MathWorks with disp('do not use eval').
Steven Lord
Steven Lord 2016년 9월 9일
You are correct that eval is a valid MATLAB command. But the key point to remember is that not all tools are appropriate in all circumstances. A chainsaw is a wonderful tool, appropriate for some tasks. But you wouldn't want to use it to carve a statue unless you have a lot of experience in its use, the proper safety equipment, etc. You certainly wouldn't want to use it to cut a pizza into wedges unless you were showing off (and probably not even then.)
The main reason I discourage people from using eval is that I've seen new users learn it exists and then try to use it for everything, including tasks where another approach is more appropriate. The saying "When all you have is a hammer, everything looks like a nail." definitely applies to the hammer that is eval.
For instance, eval makes it difficult or impossible for MATLAB to optimize code (because the body of the eval could be literally any MATLAB code) and so performance could suffer. A second reason to be cautious with eval is that if you use eval to execute user-provided code, you're giving the user that provided that code complete and total access to your machine. If they want to format your hard drive and the user who executed MATLAB has permissions to do so, they can.
I have used eval myself in test code I have written. But when I do, I do so sparingly and I do so because I have determined based on my experience that it is safe and appropriate to do so and because it is preferable to the alternatives (or there are no alternatives.)

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


Isma_gp
Isma_gp 2016년 9월 9일
Hi,
When using this, eval is trying to access RW_001 instead of RW_01.
Regards
  댓글 수: 1
Thorsten
Thorsten 2016년 9월 9일
편집: Thorsten 2016년 9월 9일
Oops. I changed my code accordingly.

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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by