For loop to work on files in subfolders, while skipping the first file

I want todecode my data with tdt on MATLAB. I have a folder, this folder has 70 sub-folders. Each of the 70 sub-folders has four .mat files. I want to work on the 2nd three files while loading the first spm.mat file separately. How do I write a for loop that runs through these files so that after working on the loading the first file, I can work on each of the other 3 files sequentially then move to the next folder. These 3 files start with the letters voi (e g voi 1, voi 2). I used the folowing and got an error message: Dot indexing is not supported for variables of this type.
folders = dir('parent folder path'); % Captures all contents of parent folder
folders = folders([folders(:).isdir]==1); % Filter to just folders
folders = folders(3:end); % Remove unnecessary '.' and '..' directories
for i = 1:length(folders)
path = [folders(i).folder,'\',folders(i).name]; % do work with path variable directing to each child folder.
end
end

2 Comments

Sign in to comment.

 Accepted Answer

Here's one way:
topLevelFolder = pwd; % Wherever you want.
filePattern = fullfile(topLevelFolder, '**/*.*');
folders = dir(filePattern); % Captures all contents of parent folder and subfolders
% Filter to just folders
itsAFolder = [folders.isdir]
folders = folders(itsAFolder);
% Get all folder names in a cell array.
folderNames = {folders.folder, folders.name}'
c = contains(folderNames, topLevelFolder)
folderNames = unique(folderNames(c))
% Loop over each folder and subfolder.
for k = 1:length(folderNames)
thisFolder = folderNames{k};
fprintf('Processing folder : "%s".\n', thisFolder)
% do work with path variable directing to each child folder.
end

13 Comments

@Monalisa Chikezie I saw your edit but it doesn't look like you ever tried my solution. Why not? I tried it and it works. Will you try please it?
@Image Analyst I realized that (fprintf('Processing folder : "%s".\n', thisFolder) this line takes too long to run
@Monalisa Chikezie when I enclose fprintf in tic and toc it shows this:
tic
fprintf('Processing folder : "%s".\n', thisFolder)
toc
Elapsed time is 0.000012 seconds.
but if 12 microseconds is too long for you to wait, then you can certainly comment out that line, though maybe you should cut down on the caffeine. 😄
I think it may be for
k = 1:length(folderNames) this line. It has been running for over 2 hours. If you have a suggestion, I would appreciate it.
What is your top level folder? If it's the root it may take a while to do the dir statement. Step through the code or put in tic and toc to see what's taking up all the time. Attach your actual m-file if you need more help.
I tried using tic toc, but, it didn't work as the code refused to stop running. I had to uninstall and reinstall matlab but, still same
it gets stuck once I start the first line of the for loop
folderNames has the subfolders. Thanks for the video, I'd watch it
Then when you step through it line by line, which line hangs?

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!