MATLAB Answers

Loading .mat file into a variable

237 views (last 30 days)
Tyler on 26 Jul 2016
Edited: Stephen Cobeldick on 14 Apr 2017
I have a bunch of .mat files, each containing a specific variable that I would like to combine into one variable. Yes, I know this is not efficient coding, but in order to conserve memory I needed to save them this way. My main question is how to load each variable, when their names only differ by a number.
For example, my variables could be DataSet_1000_1_1, DataSet_1000_2_1, DataSet_1000_3_1, DataSet_1000_4_1, etc.
I have a lot of these variables, each saved in a different .mat file. I would like to avoid typing each name to combine them. This is what I've tried so far-
inJ0 = [];
for i = 1:length(Start)
% Start is a vector containing [1,2,3,4...] etc. (What I will be changing in the variable names. In my real problem they aren't evenly spaced integers).
inJ0temp = load(['F:\SuperComputerDataSets\DataSets_1000\DataSet_1000_' num2str(Start(i)) '_1'],['inJ0_1000_' num2str(Start(i)) '_1']);
inJ0 = [inJ0;inJ0temp];
In this example, J0temp is a structure with the variable I want. However, I would like to assign this variable a name that I can then use for other purposes, such as combining onto other arrays. I've tried using eval, but that doesn't seem to work. My problem is that I can't say
J0temp = J0temp.DataSet_1000_1_1;
because the number 1 would change on every loop iteration. Is there a way to do something like this?
J0temp = J0temp.DataSet_1000_num2str(Start(i))_1;
I've also tried evalin, but can't get that to work either. It's the num2str that seems to be messing me up, I just don't know how to incorporate that into other functions I guess. I've also tried using load without the J0temp assignment, but then I can't work with that variable.
load(['F:\SuperComputerDataSets\DataSets_1000\DataSet_1000_' num2str(Start(i)) '_1'],['inJ0_1000_' num2str(Start(i)) '_1']);
J0temp = ['inJ0_1000_' num2str(Start(i)) '_1']; % Doesn't work
Any help is appreciated, thanks.

Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 26 Jul 2016
Edited: Stephen Cobeldick on 26 Jul 2016
It is simple to put the data into one cell array:
% Save fake data:
data1 = 1:3;
data2 = 3:5;
data3 = 5:7;
data4 = 7:9;
% Load data:
S = dir('data*.mat');
N = {};
Z = cell(1,numel(N));
for k = 1:numel(N)
Z(k) = struct2cell(load(N{k},'data*'));
And then accessing the data in the cell array is simple:
>> Z{2}
ans =
3 4 5
>> vertcat(Z{:})
ans =
1 2 3
3 4 5
5 6 7
7 8 9
You could also, if the arrays are of a suitable size, simply preallocate a numeric array and allocate directly into that.
Note also that I used a regular expression for the second argument of load: this removes the need to explicitly specify the variable name (as long as only one variable name will match this expression then this will work).
Stephen Cobeldick
Stephen Cobeldick on 24 Aug 2016
@Tyler Critchfield: eval is not a good solution to this problem, no matter how much beginners love using it:
You just picked a "solution" that makes your code slower, buggier, and more difficult to manage. By having lots of separate (numbered) variables in the workspace you make it harder to access them, something that it is trivial when they are stored in one variable (like my answer shows you). If you are at all interested in learning how to write reliable, fast, and readable code, then you should reconsider using eval and pick a better method:
"This solution didn't work" I showed a working example. If your code is incorrect then please show us what you have attempted and we can help you fix it.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!