Azzera filtri
Azzera filtri

load files from different folders in current folder

4 visualizzazioni (ultimi 30 giorni)
Hello
I have 3 different folders in the current folder (CBC, MHA, FJR). Each folder contains 12 mat files which have the same names in all folders. I have some code, that load and make some data processing on 12 files from one folder. Now I want to make a for loop that perform data processing in all three folders. I would like something like open the folder, make data processing and go back to current folder, then open another folder and so on. How can I do that?

Risposta accettata

Walter Roberson
Walter Roberson il 31 Ott 2019
projectdir = '.'; %or name of containing folder
foldinfo = dinfo(projectdir);
foldinfo(~[foldinfo.isfolder]) = []; %get rid of non-folders
foldinfo(ismember({foldinfo.name}, {'.', '..'})) = []; %get rid of . and .. folders
foldnames = fullfile(projectdir, {foldinfo.name});
numfold = length(foldnames);
for D = 1 : numfold
thisfold = foldnames{D};
dinfo = dir( fullfile(thisfold, '*.mat'));
filenames = fullfile(thisfold, {dinfo.name});
numfile = length(filenames);
for F = 1 : numfile
thisfile = filenames{F};
file_struct = load(thisfile);
%now extract variables from file_struct and process them
end
end
  3 Commenti
Zakia wani
Zakia wani il 10 Gen 2022
can u plz mention where to edit this code to use it
Walter Roberson
Walter Roberson il 10 Gen 2022
Edit the line
projectdir = '.'; %or name of containing folder
to reflect the name of the parent directory that contains the subfolders.
Edit the line
dinfo = dir( fullfile(thisfold, '*.mat'));
to change .mat to the appropriate extension for the files you are using.
Edit the line
file_struct = load(thisfile);
to use whatever function is needed to read in those kinds of files. For example, perhaps readtable() or dicomread(), depending on what is stored in the file.
Edit the line
%now extract variables from file_struct and process them
to be whatever code you need in order to process the file that you just read in.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by