Run script on all subfolders

조회 수: 11 (최근 30일)
Alexis Haas
Alexis Haas 2019년 4월 30일
답변: Alexis Haas 2019년 5월 1일
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
Rik
Rik 2019년 4월 30일
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 2019년 5월 1일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Alexis Haas
Alexis Haas 2019년 5월 1일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by