Loading Multiple .mat files, but not into structure
37 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have found many codes for loading multiple .mat files into the workspace, but it loads them into a structure. I would like the files loaded into the workspace as separate matrices and each named differently for each file (i.e. shape1, shape2 etc. I have 8000 files and would eventually like to take the average each grid cell between all the 8000 matrices (not sure how to do this within a structure though, so I prefer to load them all as separate matrices in the workspace).
Thank you
0 Kommentare
Akzeptierte Antwort
Stephen23
am 7 Jul. 2016
Bearbeitet: Stephen23
am 11 Dez. 2019
"not sure how to do this within a structure though"
So now is a good time to learn! Instead of learning the worst way of doing this task, why not take the opportunity of learning an efficient and robust way of doing this task ?
Creating lots of variables is possible, but it would be the slowest, buggiest, and very complicated solution. What do you imagine that you are going to do with 8000 variable in your workspace ? Loop over them and join them together ? Hint: this is possible, but very slow, buggy, and hard to debug. Read this to know why:
So load returns data in a structure, but it is easy to join them together. Here is an example, first we create some fake data:
>> A = 1:3;
>> save('file1.mat','A');
>> A = 4:6;
>> save('file2.mat','A');
>> A = 7:9;
>> save('file3.mat','A');
Now we get a list of those mat files, and read them in a loop:
>> S = dir('*.mat');
>> for k = 1:numel(S), tmp = load(S(k).name); S(k).A = tmp.A; end
Now all the data is in structure S, and it is easy to access:
>> vertcat(S.A)
ans =
1 2 3
4 5 6
7 8 9
>> S(3).A
ans =
7 8 9
You should definitely read this: http://www.mathworks.com/help/matlab/matlab_prog/access-multiple-elements-of-a-nonscalar-struct-array.html, and practice with the examples.
Of course you don't have to store the data in a structure, it is also trivial to store them in an array:
>> S = dir('*.mat');
>> M = NaN(numel(S),3);
>> for k = 1:numel(S), tmp = load(S(k).name); M(k,:) = tmp.A; end
>> M
M =
1 2 3
4 5 6
7 8 9
2 Kommentare
Stephen23
am 8 Jul. 2016
@Vespi1: in general you should use the simplest array that can be reasonably used to store the data. This will make accessing and processing the data much simpler. The simpler the array, the simpler the code. The simpler the code, the easier it is to write, to check, and to run.
If your data are numeric and have matching sizes that can be concatenated into one array (which may be an ND array), then by all means store them in a numeric array. If the data arrays have different sizes, then perhaps a cell array.
Once your data are in one array then processing becomes much faster and simpler, because you can perform operations on all of the data at once (see code vectorization), or run loops over its dimensions.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!