MATLAB: Hi i’m new to Matlab. i have directory : C:\D\Test and inside the ”Test” i got sub folders A,B,C,D and E. i have a m.file called ”test.m” in each sub folder. i wanna automate the run each test.m starting from A and then until E .

automate the code in each sub-folder

here is the code I tried before, however it does not work.
dirinfo = dir('C:\D\Test');
dirinfo(~[dirinfo.isdir]) = [];%remove non-directories
subdirinfo = cell(length(dirinfo));
for i = 1 : length(dirinfo)
thisdir = dirinfo(i).name;
subdirinfo{i} = dir(fullfile(thisdir,'test.m'));
run(fullfile(thisdir,'test.m'));
end
here is the error message I get:
Error using run (line 73)
.\test.m not found.
Error in fish (line 7)
run(fullfile(thisdir,'test.m'));

Best Answer

You also have to remove the '.' and the '..' directories that are returned by dir
dirinfo = dir('C:\D\Test');
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
dirinfo(endsWith({dirinfo.name}, '.')) = []; %removes the '.' and '..' directories