execute a function in several directories

how do I execute scritp.m function in different directories without having to navigate to each individual directory and run the script there?

 채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 8일

0 개 추천

DIRNAMES = {'uline','cool','crinkle','organic', 'mingus'}; %change appropriately
initialdir = pwd();
for dirname = DIRNAMES
cd(dirname);
scritp %the script to run
cd(initialdir)
end
The changing back to the initial dir is important if the directory names are in relative path format as in the example values I give.

댓글 수: 5

Daniel Shub
Daniel Shub 2011년 11월 8일
I think you need to add initialdir to the path so that script (scritp) can be found after you change the directory.
Baba
Baba 2011년 11월 8일
I'm getting the following error when running your code:
Argument must contain a string.
Error in ==> Master at 5'
cd(dirname);
Walter Roberson
Walter Roberson 2011년 11월 8일
Oh, right, should be
cd(dirname{1})
Baba
Baba 2011년 11월 8일
also, the DIRNAMES, do I have to include the full directory starting with the Drive letter?
Walter Roberson
Walter Roberson 2011년 11월 8일
Please expand the comments here to see Daniel's explanation of how to have the script be found.
For the DIRNAMES, you can either use full pathnames complete with drive letter, or you can use pathnames that are relative to the directory that you will be starting the script in.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2011년 11월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by