Run a code multiple times in different directories

Dear Matlab Users
I have 100 files which ordered by name like i_1 to i_100 and in each file I have a text file which called input.txt and a run.m file
which each time I shoud go to each directory and run this run.m which give me an output.txt by doing some changes in input.txt/ and this takes so long
My question is that how can I write a code in matlab which goes to each file and run the run.m file and save my output in that directory automatically!!!!
Thanks in advance for your help

댓글 수: 6

I think for loop will be cool
The link provided by Walter has complete and sweet answers to the question.
salman
salman 2019년 9월 3일
편집: salman 2019년 9월 3일
Dear All, thanks for your help,
I used this code for doing my work, but it has a problem for openning and running the .m file in the folder, any idea??
D = 'H:\Projrct\list';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 20:25
T = dir(fullfile(D,N{ii},'*')); % improve by specifying the file extension.
savename = "run";
f = fopen([savename '.m'],'wt');
fclose(f);
run(savename);
end
Thanks in advance
Stephen23
Stephen23 2019년 9월 3일
편집: Stephen23 2019년 9월 3일
"... and a run.m file"
Is run.m a script or a function?
I suspect that the name run.m will cause you problems. You should change their names.
"I wrote this code ..."
Actually you copied one of my answers, including the comments verbatim:
run.m is just an script which use some other functions in that file for doing some analysis in .txt file

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

답변 (2개)

Stephen23
Stephen23 2019년 9월 3일
편집: Stephen23 2019년 9월 3일
"but it has a problem for openning and running the .m file in the folder, any idea??"
Your code opens a text file, and then closes it immediately, without writing anything to the file:
savename = "run";
f = fopen([savename '.m'],'wt');
fclose(f);
This means that run.m is totally empty (because you just discarded any content with the w option).
Then, immediately after creating a totally empty script, you run it:
run(savename);
What do you expect running an empty script should do? I would not expect it to do anything.
salman
salman 2019년 9월 5일

0 개 추천

F=('E:\New_folder');
name=('ru.m'); % Name RUN
start=71; % Start Number
n=100; % End Number
%% Go to Address and RUN
L=string(F);
for i=start:1:n
L=string(F);
D=L+i;
cd (D);
run (name);
disp (i);
clear a b c
end

댓글 수: 1

Stephen23
Stephen23 2019년 9월 5일
편집: Stephen23 2019년 9월 5일
Avoid using cd in code like that.
It is not required to change directories to process data files: all MATLAB functions that read/write data files accept absolute/relative filenames.

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

카테고리

도움말 센터File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2019년 8월 28일

편집:

2019년 9월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by