필터 지우기
필터 지우기

Execute script on multiple Files

조회 수: 2 (최근 30일)
Nainpreet
Nainpreet 2023년 4월 13일
댓글: Nainpreet 2023년 4월 13일

Hey,
i have multiple folders, within every folder is a .txt file. I need to run a script on every .txt file independently and need the output in the same folder as the .txt file is.

  댓글 수: 2
Stephen23
Stephen23 2023년 4월 13일
The MATLAB approach:
P = 'absolute or relative path to where the subfolders are';
S = dir(fullfile(P,'*','*.txt'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
% your code here, to process file F
output = ..
G = fullfile(S(k).folder,'output.txt')
% save output data in file G
end
Nainpreet
Nainpreet 2023년 4월 13일
i tried to implement it, but it some how doesnt work. Does it also work with the import feature ? I attached my code

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2023년 4월 13일
편집: Stephen23 2023년 4월 13일
With many assumptions, e.g. that the required folders are all subfolders of one parent folder.
You should also pay attention to the advice you got for your prior questions, e.g.:
opts = delimitedTextImportOptions("NumVariables", 9, "Encoding", "UTF16-LE");
% Specify range and delimiter
opts.DataLines = [30, Inf];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["Name", "Frequenz", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"];
opts.SelectedVariableNames = ["Name", "Frequenz"];
opts.VariableTypes = ["double", "double", "string", "string", "string", "string", "string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"], "EmptyFieldRule", "auto");
P = '\\DEDOSAN001\vol1\E\EMV\Kularia\Matlabprogramm\04_Messungen';
S = dir(fullfile(P,'*','Ergebnistabelle.txt'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F, opts);
G = fullfile(S(k).folder,'RA1_AV.asc')
fid = fopen(G,'wt');
fprintf(fid, '%3.4f\t%f\n', [T.Name,T.Frequenz].');
fclose(fid);
end
  댓글 수: 1
Nainpreet
Nainpreet 2023년 4월 13일
it works perfect thank you. Yeah im pretty new to all of this, but i try to keep it in mind :D

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by