How to alter data (multiply) from all the columns of multiple text files in a folder

3 views (last 30 days)
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']);

Accepted Answer

Mathieu NOE
Mathieu NOE on 19 Jul 2021
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 (
I found to save the data that importdata is a bit faster than readmatrix , I leave it to you to choose the best option;
% reading of data txt files
Folder = cd;
FileList = dir (fullfile(Folder, 'Path1_Step_*.txt'));
FileList_sorted = natsortfiles({}); % 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
data{iFile} = importdata(FileNamePath); % faster
s{iFile} = readmatrix (FileNamePath); % slower

Sign in to comment.

More Answers (0)


Find more on Graphics Object Programming in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!