Run script on all subfolders

11 views (last 30 days)
Alexis Haas
Alexis Haas on 30 Apr 2019
Answered: Alexis Haas on 1 May 2019
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
  2 Comments
Rik
Rik on 30 Apr 2019
It is probably a much better idea to turn your script into a function that accepts a folder name. Then you can write a separate function/script that uses dir to find all the relevant folders and subfolders and pass that on to your function.
Rik
Rik on 1 May 2019
Glad to be of help. I would personally try to avoid cd, but that will probably require several changes to your script.
You can also post your solution in the answer field, so future readers can easily see this problem was solved.

Sign in to comment.

Accepted Answer

Alexis Haas
Alexis Haas on 1 May 2019
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

More Answers (0)

Categories

Find more on File Operations 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!