How to run a code (in series) in different folders?

3 views (last 30 days)
Hi. In my current folder ('C:\Run\Analysis'), I have a MATLAB file S4.m which contains a few functions to be run. The input files (in .txt format) required to run this code are in 3 different subfolders (i.e., F1, F2, F3). I need to loop my code so that it first gets .txt files from subfolder F1, run the necessary functions, save the output files then proceed to get the .txt files from subfolder F2, and so on. I read the available answers but couldn't solve the issue.
Any help will be appreciated. Thank you.
  4 Comments
M Yousaf
M Yousaf on 1 Sep 2024
@Stephen23 That's exactly what I am trying to do but in vain.
M Yousaf
M Yousaf on 1 Sep 2024
@Image Analyst Hi. Thank you for responding. Well, I couldn't know how to integrate @Star Strider's code into mine. That is why I posted my entire code to seek more help. Please see my latest comment. You can find my code there.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 1 Sep 2024
Probably the easiest way would be to create ‘S4’ as a function and go from there.
Example —
pwd
ans = '/users/mss.system.tmzz7'
A = randi(10, 5)
A = 5x5
4 3 6 1 6 7 4 9 2 7 9 6 7 4 7 5 1 4 2 2 7 7 2 10 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
writematrix(A, fullfile(pwd,'F1'))
B = randi([11 20], 5)
B = 5x5
19 18 11 14 18 19 20 12 12 12 12 15 16 12 12 16 11 18 18 12 17 19 13 14 14
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
writematrix(B, fullfile(pwd,'F2'))
C = randi([21 30], 5)
C = 5x5
21 26 28 24 24 24 28 22 25 30 22 28 29 29 23 21 27 21 23 24 28 29 26 23 22
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
writematrix(C, fullfile(pwd,'F3'))
Dirs = ["F1","F2","F3"];
for k = 1:3
fullpath = fullfile(pwd,Dirs(k))
data = readmatrix(fullpath)
% data = readmatrix(fullfile(pwd,Dirs(k))) % Single Line Version
[result1{k},result2{k}] = S4(data);
end
fullpath = "/users/mss.system.tmzz7/F1"
data = 5x5
4 3 6 1 6 7 4 9 2 7 9 6 7 4 7 5 1 4 2 2 7 7 2 10 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fullpath = "/users/mss.system.tmzz7/F2"
data = 5x5
19 18 11 14 18 19 20 12 12 12 12 15 16 12 12 16 11 18 18 12 17 19 13 14 14
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fullpath = "/users/mss.system.tmzz7/F3"
data = 5x5
21 26 28 24 24 24 28 22 25 30 22 28 29 29 23 21 27 21 23 24 28 29 26 23 22
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result1
result1 = 1x3 cell array
{[128]} {[374]} {[627]}
result2
result2 = 1x3 cell array
{[7.5880e+15]} {[1.4539e+29]} {[8.1687e+34]}
function [out1, out2] = S4(in)
out1 = sum(in, 'all');
out2 = prod(in, 'all');
end
That iis the approach I would use.
.
  4 Comments
Steven Lord
Steven Lord on 1 Sep 2024
So you don't have code in different folders, you have data in different folders?
If you have the folder names and file names in different arrays, assemble them using the fullfile function.
directoryName = fullfile(matlabroot, 'toolbox', 'matlab', 'general')
directoryName = '/MATLAB/toolbox/matlab/general'
fileName = 'bench.dat';
fullName = fullfile(directoryName, fileName)
fullName = '/MATLAB/toolbox/matlab/general/bench.dat'
Here's one way you can use that full name:
fid = fopen(fullName, 'rt');
firstLine = fgetl(fid)
firstLine = 'MATLAB(R) Benchmark Data.'
fclose(fid);
Star Strider
Star Strider on 1 Sep 2024
I would use a loop to return the complete paths to each text file, aggregatee them in ‘ImageList’ and then present tthatt to your funciton.
I am not certain what your code is doing, however this seems to work with iit —
pwd
ans = '/users/mss.system.jrszq'
[status,msg] = mkdir(fullfile(pwd,'F1'));
[status,msg] = mkdir(fullfile(pwd,'F2'));
[status,msg] = mkdir(fullfile(pwd,'F3'));
A = randi(10, 5);
writematrix(A, fullfile(pwd,'F1','image1.txt'))
B = randi([11 20], 5);
writematrix(B, fullfile(pwd,'F2','image2.txt'))
C = randi([21 30], 5);
writematrix(C, fullfile(pwd,'F3','image3.txt'))
Dirs = ["F1","F2","F3"];
for k = 1:3 % Retrieve File Paths For All Subdirectories
fullpath = fullfile(pwd,Dirs(k),"image"+k+".txt");
ImageList{k} = fullpath
end
ImageList = 1x1 cell array
{["/users/mss.system.jrszq/F1/image1.txt"]}
ImageList = 1x2 cell array
{["/users/mss.system.jrszq/F1/image1.txt"]} {["/users/mss.system.jrszq/F2/image2.txt"]}
ImageList = 1x3 cell array
{["/users/mss.system.jrszq/F1/image1.txt"]} {["/users/mss.system.jrszq/F2/image2.txt"]} {["/users/mss.system.jrszq/F3/image3.txt"]}
% ImageList={}; % Usually I put as ImageList = {'Image1.txt','Image2.txt'}
% AcceptTol={};
for iter=1:length(ImageList)
nSteps=getNSteps(ImageList{iter});
[counter,currTol]=doAnalysis(ImageList{iter},nSteps);
AcceptTol(iter,1:3)={ImageList{iter},counter,currTol};
disp(ImageList{iter});
disp(['Counter:' num2str(counter)]);
disp(['Tol:' num2str(currTol)]);
end
/users/mss.system.jrszq/F1/image1.txt
Counter:0
Tol:0.0001
/users/mss.system.jrszq/F2/image2.txt
Counter:0
Tol:0.0001
/users/mss.system.jrszq/F3/image3.txt
Counter:0
Tol:0.0001
function [counter,currTol]=doAnalysis(Image,nSteps)
OK=1; % Image contains all files e.g., Image1.txt, Image2.txt, ...
TolIncr=1e-3;
currTol=1e-4;
counter=0;
while(OK~=1)
if exist('result.txt','file')~=1
delete('result.txt');
end
fid=fopen('Imagery.tcl');
txtData='';
tline=fgetl(fid);
while ischar(tline)
txtData=strjoin({txtData,tline},'\n');
tline=fgetl(fid);
end
fclose(fid);
counter=counter+1;
if counter==1
currTol=1e-4;
elseif counter==2
currTol=5e-4;
elseif counter==3
currTol=1e-3;
else
currTol=currTol+TolIncr;
end
txtData=replace(txtData,'{TOLERANCE}',num2str(currTol));
txtData=replace(txtData,'{Image}',Image);
txtData=replace(txtData,'{nSteps}',num2str(nSteps));
fid=fopen('Imagery_Temp.tcl','w');
fprintf(fid,'%s\n',txtData);
fclose(fid);
system('program1.exe S4.tcl');
fid=fopen('result.txt','r');
OK=fscanf(fid,'%d');
end
end
function nSteps=getNSteps(fname)
fid=fopen(fname);
nSteps=0;
txtData='';
tline=fgetl(fid);
while ischar(tline)
nSteps=nSteps+1;
txtData=strjoin({txtData,tline},'\n');
tline=fgetl(fid);
end
end
My iimporovement to it simply pulls the relevant paths and fiile names from the various directories, then saves them to a cell array that your code then uses to so what ever it is that you want iit to do.
You may have to tweak it to work with your files and directories, however this simulation seems to work.
.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!