Execute script on multiple Files

2 次查看(过去 30 天)

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