Load different mat files using for loop

Hello guys!! I'm trying to load several subjects from a folder, with the data of those subjects having different mat filenames.
What I have implemented at the moment is this:
DataLocation = 'D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\Leuven';
FileType = fullfile(DataLocation, '*.mat');
dirFicheiroMat = dir(FileType);
number_subjects = length(dirFicheiroMat);
for i=1:number_subjects
baseFileName = fullfile(DataLocation, dirFicheiroMat(i).name);
lfp = load(baseFileName);
end
Where lfp variable will be used after, to compute some calculation for each subject iterated.
But the problem of this code lies in the fact that lfp will be a structure after each iteration, and I want that variable to store the "true" values of each subject, instead of a structure...
Can anyone give me any help in this?
Thanks in advance,
Hugo

2 Comments

" I want that variable to store the "true" values of each subject, instead of a structure..."
Sure.
But mat files can contain any number of variables with any (permitted) names. So far you have not told us how many variables there are in the mat files nor what their names are, which makes it difficult for us to help you.
Sorry about that...
So basically in each folder I have several mat files, with these being respective to one subject, and they contain a 2D matrix (between each mat file the size can be different) with the values of fMRI BOLD time-series.
The names of these files are from this type "Leuven10050682roisaal, Leuven20050726roisaal" and so on.

Sign in to comment.

 Accepted Answer

Assuming that each mat file contains exactly one variable of unknown name, then try this:
D = 'D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\Leuven';
S = dir(fullfile(D,'*.mat'););
for k = 1:numel(S)
F = fullfile(D, S(k).name);
C = struct2cell(load(F));
S(k).data = C{1};
end
You can trivially access the file data using indexing, e.g. the second file:
S(2).data
If your mat files actually contain more than one variable then you will need to give us more information: how many, their names, etc.

6 Comments

Yes I'm dealing with mat files with only one variable so the first option worked perfectly!!
Thank you so much Stephen!
Sorry to bother you again Stephen, that problem metioned before is solved but I got an error after.
So I adapted my code with the help you gave me and it's like this:
DataLocation = 'D:\ABIDEdataset\Outputs\dparsf\nofilt_noglobal\rois_aal\Leuven';
FileType = fullfile(DataLocation, '*.mat');
dirFicheiroMat = dir(FileType);
number_subjects = length(dirFicheiroMat);
ConnectivityMatrix_Corr = zeros(116,116,number_subjects);
modelOrder = 10;
for i=1:number_subjects
baseFileName = fullfile(DataLocation, dirFicheiroMat(i).name);
baseFileName_cell = struct2cell(load(baseFileName));
dirFicheiroMat(i).data = baseFileName_cell{1};
lfp = transpose(dirFicheiroMat(i).data);
Now I wanted to use that lfp (which contains the time-series values of each subject) to compute a calculation, inside the loop, which is this:
ConnectivityMatrix_Corr(:,:,i) = mln_icalcMatTimeBasic(lfp,modelOrder);
where ConnectivityMatrix_Corr is a 3D matrix and in the last dimension (which is the size of the total number of subjects in that folder) I want to store the results from that calculation for each subject.
But I got this error:
Conversion to double from struct is not possible.
Error in CalcularMetricas2 (line 35)
ConnectivityMatrix_Corr(:,:,i) = mln_icalcMatTimeBasic(lfp,modelOrder);
Can you give me a hint about what is wrong?
Thanks in advance!
"Can you give me a hint about what is wrong?"
mln_icalcMatTimeBasic's output is a structure.
ConnectivityMatrix_Corr has class double.
Iugo
Iugo on 20 Feb 2021
Edited: Iugo on 20 Feb 2021
How can I save each subject's output (which are a structure) in that ConnectivityMatrix_Corr third dimension? Or, if possible, how can I save each field of the structure for every subject in different matrices?
I've never done anything like this before ...
Stephen23
Stephen23 on 20 Feb 2021
Edited: Stephen23 on 20 Feb 2021
"How can I save each subject's output (which are a structure) in that ConnectivityMatrix_Corr third dimension?"
I don't know, because I have no idea about the structure size, how many fields the structure has, what classes the fields are, or what sizes the fields might have. If at all possible, the answer to your question would be to extract the data from the fields of that structure and allocate them to the desired array.
"Or, if possible, how can I save each field of the structure for every subject in different matrices?"
They already are different matrices, referenced by the fieldname/index of that structure.
Rather than putting the data into one numeric matrix, perhaps using a non-scalar structure might be better:
Thank you for your help @Stephen Cobeldick!!
I'll check those files.
Best regards,
Hugo

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Feb 2021

Commented:

on 20 Feb 2021

Community Treasure Hunt

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

Start Hunting!