Import matrices with same name and dimensions from different Data

6 views (last 30 days)
Hey there,
I need Help.
I've been struggling with the import of matrices in my Workspace.
I have a folder with 10 repetitions of matrices (X and c )having the same name but with different values in each repetition.
To bring the matrices to the workspace i've uses this loop :
for idx = 1:10
file = strcat("diff_0_adh_1_rep_", num2str (idx) ,".mat");
load (file);
end
This has only resulted in bringing the matrices (X and c) of the last repetition (10th) in my workspace.
The matrices of the first 9 repetitions aren't there ; they've been overwritten due to their same name.
What should i do ?
Thanks

Answers (2)

Stephen23
Stephen23 on 15 Dec 2020
Edited: Stephen23 on 15 Dec 2020
"What should i do ?"
Load into an output variable, not directly into the workspace.
Loading into an output variable will avoid possible bugs and makes your code much more robust.
D = 'absolute or relativve path to where the files are saved'; % !!! CHANGE THIS !!!
N = 10;
C = cell(1,N); % preallocate
for idx = 1:N
F = sprintf('diff_0_adh_1_rep_%d.mat',idx); % more robust than NUM2STR and STRCAT
C{idx} = load(fullfile(D,F)); % load into a variable
end
S = [C{:}]; % concatenate into one structure array
Note that the concatenation will only work if all files contain the same variables.
You can simply use indexing and the fieldnames to access the data, e.g. for the second file:
S(2).X
S(2).c
Read more:

Sindar
Sindar on 15 Dec 2020
If you don't tell matlab what to do with the variables, it will simply overwrite them each time. There are a few options
Structure Array
The simplest is to load into a structure array:
for idx = 1:10
file = strcat("diff_0_adh_1_rep_", num2str(idx) ,".mat");
% only load the variables you want, in case others are present
data(idx) = load(file,'X','c');
end
Then, the data will be accessible like so:
% first X
data(1).X
% 7th c
data(7).c
This is useful if the relationship between the 1st 'X' and first 'c' is more relevant than between different X's or c's, and extends well to many variables in the files
Variables with Pages
Assuming each 'X' and 'c' have the same size, you can separate them by a third dimension, pages:
for idx = 1:10
file = strcat("diff_0_adh_1_rep_", num2str(idx) ,".mat");
% load data into a temporary structure
data = load(file,'X','c');
% pre-allocate based on the sizes of the first loaded in
if idx==1
X = zeros(size(data.X,1),size(data.X,2),10);
c = zeros(size(data.c,1),size(data.c,2),10);
end
% place the data into the corresponding page
X(:,:,idx) = data.X;
c(:,:,idx) = data.c;
end
Then, the data will be accessible like so:
% first 'X'
X(:,:,1)
% 7th 'c'
c(:,:,7)
paged variables allow you to do a lot of things more efficiently, like take the mean for each element across pages:
meanX = mean(X,3);
Cell Arrays
If each 'X' and 'c' have different sizes (or types), storing them in matrix pages won't work, but cell arrays will:
for idx = 1:10
file = strcat("diff_0_adh_1_rep_", num2str(idx) ,".mat");
% load data into a temporary structure
data = load(file,'X','c');
% no preallocation since you don't know how much space you need
% place the data into the corresponding cell
X{idx} = data.X;
c{idx} = data.c;
end
Then, the data will be accessible like so:
% first 'X'
X{1}
% 7th 'c'
c{7}

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!