Use a for loop to find files in subdirectories for own function, and then extract data from .csv file

2 views (last 30 days)
I have nearly 700 folders, which all contain 3 files that I need for a pre-defined function that my lab uses (basically Dice and Jaccard calculations for image processing).
My directory looks somewhat like this:
  • PID000001
  • PID000001_Hipp.nii.gz
  • PID000001_Hipp2.nii.gz
  • PID000001_ICV.nii.gz
  • PID000002
  • PID000002_Hipp.nii.gz
  • PID000002_Hipp2.nii.gz
  • PID000002_ICV.nii.gz
and so on.
The function looks like:
function(inputfile1, inputfile2, inputfile3, output.csv)
% A single example would be like
validate_segmentation_results('PID000001_Hipp.nii.gz', 'PID0000001_Hipp2.nii.gz', 'PID0000001_ICV.nii.gz', 'PID000001_dat.csv')
% where the first 3 are the input files, and the last is the output file
I need to create a for loop that says for each subfolder, search for the files that have _Hipp, _Hipp2 and _ICV in their names, and use them in the function as inputfile1, inputfile2 and inputfile3 respesctively. This should then create a .csv file in every subfolder. I believe it should look something like this (based on a previous loop I have had help with):
% First describe the path using dir and save to a variable name.
files = dir('R:\path\where\subfolders\are');
% Next, loop through all the subdirectories and perform validation function
for i in 1:length(files)
% Create variable that contains a list of all the subdirectores i.
PID_1 = files{i};
PID_1name = {}
% Identify which files contain 'Hipp', 'Hipp2' and 'ICV' in names.
% Perform validation function
validate_segmentation_results() % this is the function with 3 inputs and 1 output.
% Finally, loop through all the directories to find the output file.
% The output file is in long format i.e. varname in col1, value in col2.
% Transform this to wide format i.e. varname on row1, value in row2.
for j in 1:length(files)
% Create variable that contains a list of all the subdirectories j
PID_2 = files{j};
PID_2name = {};
% Identify which files end in '.txt' in file names.
% Use fid to open the files, then transform the data from long to wide.
However before the end, I get stuck. I know I should be using something akin to regular expression, but there are gaps in the code because I don't know what I should be typing. Please help!

Answers (1)

Mathieu NOE
Mathieu NOE on 10 Jan 2023
Edited: Mathieu NOE on 10 Jan 2023
I would suggest this code to find the files in each folder
then you can introduce your own processing of the identified files (for each folder)
%NB : data files are not searched inside the main folder (yourpath) but in
% all subfolders (dirnames)
% if S is empty that means no data file (matching the file filter is in the searched subfolder)
%% define path
yourpath = pwd; % or your specific path
list=dir(yourpath); %get info of files/folders in current directory
isfile=~[list.isdir]; %determine index of files vs folders
dirnames={list([list.isdir]).name}; % directories names (including . and ..)
dirnames=dirnames(~(strcmp('.',dirnames)|strcmp('..',dirnames))); % remove . and .. directories names from list
%% Loop on each subfolder
for ci = 1:length(dirnames) %
fileDir = char(dirnames(ci)) % current directory name
S = dir(fullfile(fileDir,'*.gz')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab does not) , see FEX :
%% Loop inside folder
for k = 1:length(S) % read data in specified sheet
currentFilename = S(k).name;
if contains(currentFilename,'_Hipp.')
filename1 = currentFilename
if contains(currentFilename,'_Hipp2.')
filename2 = currentFilename
if contains(currentFilename,'_ICV.')
filename3 = currentFilename
%% Your own code for filename1,filename2,filename3

Community Treasure Hunt

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

Start Hunting!