Running of mathlab files in different subfolder automatically

2 views (last 30 days)
Hi, I am writing a piece of code which opens subfolders ( Simulation_1 to Simulations_10 ) and runs a file named: ( A_File_to_run_programs.m) automatically.
The problem: the current folder stays in the parent directory and not in the folder Simulation_1 to Simulation_10
Thanks for your help in advance.
Hypolite
  1 Comment
Stephen23
Stephen23 on 6 May 2018
Edited: Stephen23 on 6 May 2018
What you are trying to do is slow and complex: copying files in order to run them is not required, because all MATLAB functions that accept filenames will accept absolute/relative filenames, so all you need to do is add the file path to the name, before passing it to whatever function needs to use it.
Do NOT:
  • Use slow cd
  • Copy code/mfiles into data folders just so that they can be run on some data.
Instead simply write your code so that you correctly pass the directory path with the filenames, and your code will be simpler, more efficient, and easier to debug.
DO:
  • Keep your code is one place, separate from your data.
  • Use absolute/relative filenames.

Sign in to comment.

Accepted Answer

Hypolite Richardson
Hypolite Richardson on 6 May 2018
Hi Stephan,
I am a beginner so thanks for the information, pertaining to debugging and how to really use mathlab correctly.
I will strucute my code different, so as you said will pass thru the directory path.
Thanks a million.
Hypolite

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!