How to run a .m script for all folders in a directory?

Hello,
I have a question about a code. I have a .m script. This script requires 2 inputs in a folder in order to run. But I would like to run this .m script for all folders in a directory (path). The reason is that each input has different numbers so different results of each run of .m code will arise.
Could you please help me?

10 Comments

Why are you using scripts? This would probably be easy with a function that accepts a path.
Because I wanted to make my calculation for only one folder, but now I would like top do it for all my folders in a directory...
Could you please help me?
if i understood you correctly your script simply works in the current directory. what you can do is that you add your script to your search path ( addpath() ) and then navigate to the folders using cd() and calling your script
ok Bbut my point is that if I could do it in an autimated manner. I would like to use some commands that will seach one to one my folders and run my script for each folders.
Is there a way to make it?
e.g. if all folders in which you want to run the script are on the same director level you could use something like
addpath('MyScript.m')
dirContent=dir(); % get content of current directory
dirContent(~[dirContent.isdir])=[]; % delete entries that are no folders
dirContent(1:2)=[]; % delete '.' and '..' directory
folders={dirContent.name} % get folder names
for dirNr=1:numel(folders)
cd(folders{dirNr}); % change dir to specified folder
MyScript();
cd ..; % go back up a level
end
NOTE: this loop should not be changed to a parfor loop because changing directories causes some issues in that case
You really should avoid messing with the path only to read/process files.
You should convert your script to a function that accepts a path. If you need help with that, feel free to comment with your issues. If you attach your script to a comment we may be able to give you more targeted advice.
@Jonas I used you script but command window shows me:
Undefined variable "folders" or class "folders".
Error in test_code (line 11)
cd(folders{dirNr}); % change dir to specified folder
What should I do?
In order to explain. In each folder I have:
2 input files (one .txt and one .xlsx that are required for my .m code) and
1 matlab code.
I would lik e to find a way in order to run the matlab code first for the firts folder, second for the second etc.
What should I do?
You should convert your matlab code to a function that accepts a path.
You should not use scripts for actual work. You should avoid cd, it is almost never required.
Attach your script so we can suggest edits. If it is just reading two files, that should not be hard.
"What should I do?"
Use functions.
Use absolute/relative filenames.
Avoid CD.
Avoid scripts for serious work (i.e. for anything versatile, expandable, repeatable, reliable, testable, ...)

Sign in to comment.

Answers (0)

Categories

Asked:

on 21 Apr 2021

Commented:

on 22 Apr 2021

Community Treasure Hunt

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

Start Hunting!