"A simple 'load' command will give me a strut, which I can't do much with."
"Ideally, I'd like a workspace containing all 96 variables structurally named, like 'J_333_01_a'. From there, I should be able to process them further (plotting etc)"
Nope, you have that exactly the wrong way around. Loading data into lots of numbered (or otherwise uniquely named) variables would be the worst way of doing this. Loading into a structure and then either
- storing the structure (could be non-scalar, if the fields are the same), or
- extracting and storing the data within the structure
would be the fastest and most robust ways to do this. Read this to know why:
The best solution is to take a deep breath, and learn to love structures. Really, you can do more with structures than you can by loading that data into independent variables. All of those things that you imagine doing: looping over your data, concatenating the data into numeric array is trivial when you have load-ed into a structure. Load them into separate variables and you will be writing the slowest, buggiest, and most obfuscated code in town.
Incidentally when you try to access variables dynamically all of MATLAB Editor's helpful tools (variable highlighting, code checking, error checking, f1 help, syntax help, etc, etc) do not work: why beginners prefer to use dynamic variable access which disables all of the MATLAB tools that help them to write good code is a mystery.
Have a look at this simple example (which I just copied from this question): 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
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
Best Answer