How to run a code (in series) in different folders?
조회 수: 5 (최근 30일)
이전 댓글 표시
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.
답변 (1개)
Star Strider
2024년 9월 1일
Probably the easiest way would be to create ‘S4’ as a function and go from there.
Example —
pwd
A = randi(10, 5)
writematrix(A, fullfile(pwd,'F1'))
B = randi([11 20], 5)
writematrix(B, fullfile(pwd,'F2'))
C = randi([21 30], 5)
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
result1
result2
function [out1, out2] = S4(in)
out1 = sum(in, 'all');
out2 = prod(in, 'all');
end
That iis the approach I would use.
.
댓글 수: 4
Steven Lord
2024년 9월 1일
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')
fileName = 'bench.dat';
fullName = fullfile(directoryName, fileName)
Here's one way you can use that full name:
fid = fopen(fullName, 'rt');
firstLine = fgetl(fid)
fclose(fid);
Star Strider
2024년 9월 1일
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
[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={}; % 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
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.
.
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!