How can I iterate through all .txt files in a folder to process them with already written code?

19 ビュー (過去 30 日間)
I have raw mass spectral data in .txt files with m/z in one column and intensity in another column. Right now, I'm importing them individually using the textread function. I know the textread function is not suggested for use anymore, and I want to speed up my processing. Is there functions that would allow me to run through all of my data in a folder, typically one day's worth of data, and process it that way? I know it would probably include a for loop, but I'm unsure how to write it.

採用された回答

Walter Roberson
Walter Roberson 2023 年 6 月 5 日
projectdir = 'location/of/text/files';
dinfo = dir(fullfile(projectdir, '*.txt'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(filenames);
for K = 1 : numfiles
thisfile = filenames{K};
thisdata = load(thisfile);
mz = thisdata(:,1); intensity = thisdata(:,2);
do something with mz and intensity
end
This assumes that the files are text files that have no headers (*) and are pure text in two columns separated by space or comma. load() of such a file will handle csv files for example.
If there are headers, then the easiest change is to switch from load() to readmatrix()
(*) Note: load() can handle headers in text file under the restricted situation that the header or comments have % as their first non-blank character on the line

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by