Access variables in several .mat files

6 ビュー (過去 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 Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by