フィルターのクリア

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 Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by