How to alter data (multiply) from all the columns of multiple text files in a folder
5 ビュー (過去 30 日間)
古いコメントを表示
I have multiple text files locate in a folder.The names of the text files varies.How shall call them & multiply all the values inside all the text files with 10^-6 in a loop.
Attached 3 text files for refernces.I've made a small code but its ineffective as file name changes and not a value of numstr.
filenames = 'Path1_Step_0001';
for i = 1:length(filenames)
s{i} = readmatrix (['Path1_Step_' numstr(i).'txt']);
end
0 件のコメント
採用された回答
Mathieu NOE
2021 年 7 月 19 日
hello
this is my suggestion ... the natural sorting order of the files may not be critical here, but it may be helpful another day , so this is a bonus ... natsortfiles is available from the FEX : Natural-Order Filename Sort - File Exchange - MATLAB Central (mathworks.com)
I found to save the data that importdata is a bit faster than readmatrix , I leave it to you to choose the best option;
clc
clearvars
% reading of data txt files
Folder = cd;
FileList = dir (fullfile(Folder, 'Path1_Step_*.txt'));
FileList_sorted = natsortfiles({FileList.name}); % sort file names into order
M = numel (FileList_sorted);
for iFile = 1:numel(FileList)
FileName = FileList_sorted{iFile};
FileNamePath = fullfile(Folder, FileName);
% load the data : choose one or the other option
tic
data{iFile} = importdata(FileNamePath); % faster
toc
tic
s{iFile} = readmatrix (FileNamePath); % slower
toc
end
12 件のコメント
Sideris Tzid
2021 年 11 月 18 日
編集済み: Sideris Tzid
2021 年 11 月 18 日
I'm sorry for my english.
Ummm actually I fixed the "last text" issue.
The problem that remains is that I have a folder which looks like this
each one of the subfolders has its own subfolders and so on.
The script that I'm running at the moment is :
clc
clearvars
Folder = 'C:\Users\sideris\Desktop\folder example';
FileList = dir (fullfile(Folder, '*.txt'));
FileList_sorted = natsortfiles({FileList.name}); % sort file names into order
M = numel (FileList_sorted);
for iFile = 1:M
FileName = FileList_sorted{iFile};
FileNamePath = fullfile(Folder,FileName);
tic
s{iFile} = readmatrix (FileNamePath);
toc
data_out = s{iFile}*0.0001;
filename_out = Folder+"\"+[FileName(1:length(FileName)-4) '_out.txt'];
writematrix(data_out, filename_out,"Delimiter","tab");
end
That script only works if all my text files are in the same folder. Is there any way to make that script read all the txt files from every subfolder?
Mathieu NOE
2021 年 11 月 18 日
hello
look there and see code provided by ImageAnalyst
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!