Load different mat files using for loop

12 views (last 30 days)
Iugo on 19 Feb 2021
Commented: Iugo on 20 Feb 2021
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);
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,
Iugo on 20 Feb 2021
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

Stephen23 on 20 Feb 2021
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};
You can trivially access the file data using indexing, e.g. the second file:
If your mat files actually contain more than one variable then you will need to give us more information: how many, their names, etc.
Iugo on 20 Feb 2021
Thank you for your help @Stephen Cobeldick!!
I'll check those files.
Best regards,

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements 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!