How to run a code (in series) in different folders?
8 ビュー (過去 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!