Access variables in several .mat files

8 views (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 10 Sep 2016
Edited: Stephen23 on 12 Apr 2017
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:

More Answers (2)

Thorsten
Thorsten on 8 Sep 2016
Edited: Thorsten on 9 Sep 2016
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 Comments
Thorsten
Thorsten on 8 Sep 2016
Edited: Thorsten on 8 Sep 2016
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 on 9 Sep 2016
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.)

Sign in to comment.


Isma_gp
Isma_gp on 9 Sep 2016
Hi,
When using this, eval is trying to access RW_001 instead of RW_01.
Regards
  1 Comment
Thorsten
Thorsten on 9 Sep 2016
Edited: Thorsten on 9 Sep 2016
Oops. I changed my code accordingly.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!