Import matrices with same name and dimensions from different Data

조회 수: 13 (최근 30일)
Skander Besbes
Skander Besbes 2020년 12월 14일
답변: Sindar 2020년 12월 15일
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

답변 (2개)

Stephen23
Stephen23 2020년 12월 15일
편집: Stephen23 2020년 12월 15일
"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 2020년 12월 15일
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}

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by