Access variables in several .mat files

6 vues (au cours des 30 derniers jours)
Isma_gp
Isma_gp le 8 Sep 2016
Modifié(e) : Stephen23 le 12 Avr 2017
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

Réponse acceptée

Stephen23
Stephen23 le 10 Sep 2016
Modifié(e) : Stephen23 le 12 Avr 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:
  1 commentaire
Isma_gp
Isma_gp le 13 Sep 2016
Thank you! very interesting.

Connectez-vous pour commenter.

Plus de réponses (2)

Thorsten
Thorsten le 8 Sep 2016
Modifié(e) : Thorsten le 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 commentaires
Thorsten
Thorsten le 8 Sep 2016
Modifié(e) : Thorsten le 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 le 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.)

Connectez-vous pour commenter.


Isma_gp
Isma_gp le 9 Sep 2016
Hi,
When using this, eval is trying to access RW_001 instead of RW_01.
Regards
  1 commentaire
Thorsten
Thorsten le 9 Sep 2016
Modifié(e) : Thorsten le 9 Sep 2016
Oops. I changed my code accordingly.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by