Run script on all subfolders
11 views (last 30 days)
Show older comments
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
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
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.
Accepted Answer
More Answers (0)
See Also
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!