MATLAB: Run script on all subfolders

folderifloopscriptsubfolder

Hi everyone,
I'm a scripting begginner, and I am very new to Matlab language so, sorry for my lack of knowledge.
My data are organised in folders, containing subfolders. Each subfolder is an analysis point.
1
1
data recognised by my script
2
data recognised by my script
3
2
1
and so on
At the moment, my script works with a 'uigetdir', where I select each subfolders manually.
(data in the subfolders are not important, the script recognises specific strings in names, and it works fine)
I'd like to select the folder, and have the script running on all subfolders, one after the other.
The trick being changing the 'uigetdir' for a 'path=pwd'. So a loop would recursively define the path to the one of each subfolder.
I attempted to write a 'for' loop around my script using different codes I found in several topics in the Q&A.
The only one that worked to some extend is here:
I copy/paste my script between these 2 lines:
oldfolder = cd(list(i).name);
my script
cd(oldfolder);
However, the script stops after the analysis of the first subfolder. I obtain an error stating the variable 'oldfolder' is not defined.
cd(oldfolder);
If I get rid of oldfolder = cd(list(i).name); then the path is stuck on the folder, and my script doesn't find the files in the subfolders.
I also tried to replace the 'i' counter from the loop by any other letter, since i is used as a counter in my script, but it did not solve the issue.
Can anybody help me ?
Thank a lot!
alex

Best Answer

Dear Rik,
Based on your suggestion, I tried this:
I created a function called automated_analysis, containing my script, with no argument.
I then wrote this script:
g=uigetdir('','Select Input-folder'); %select the input-folder that contains the subfolders
cd(g);
list = dir;
list = list([list.isdir]);
list = list(~ismember({list.name},{'.' '..'}));
l=length(list);
for i=1:l
d = cd(list(i).name);
automated_analysis
cd(d);
end
I put both script and function in the same folder and...
IT WORKS !
Thank you so much for your help !
I am a beginner so it took me few hours to make it work, but it paid well :)
Very pleased with my first Matlab community experience.
alex
Related Question