Run a code multiple times in different directories
Show older comments
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 Comments
darova
on 28 Aug 2019
I think for loop will be cool
Sulaymon Eshkabilov
on 28 Aug 2019
The link provided by Walter has complete and sweet answers to the question.
"... 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:
salman
on 3 Sep 2019
Answers (2)
"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.
Categories
Find more on Data Import and Analysis 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!