Access variables in several .mat files
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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:
更多回答(2 个)
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
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
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.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!