specific text to excel
4 次查看(过去 30 天)
显示 更早的评论
I have several text files with diffrent file name, and I need to extract specific data from those text file and record it in excel. The sample text file is below. In each text file there are 1000 collapse multiplier value and they are both positive and negative values. I have to extract this 1000 data from multiple text file and record in a excel file. Each text file have 1000 data, so for 5 text file I need 5 columsns in excel. Please help to solve it.

0 个评论
回答(1 个)
Walter Roberson
2021-8-18
filenames = {'T3.txt', 'shortrun.txt', 'scarecrow.txt', 'longrun.txt', 'T3b.txt'};
outfile = 'collapse.xlsx';
colnames = 'A':'Z'; %needs fixing if you have more than 26
for K = 1 : length(filenames)
thisfile = filenames{K};
S = fileread(thisfile);
collapses = str2double(regexp(S, '(?<=COLLAPSE MULTIPLIER =\s+)\S+', 'match'));
writematrix(collapses(:), outfile, 'range', colnames(K));
end
6 个评论
Walter Roberson
2021-8-19
Tested. Finishes in a small number of seconds.
I took the liberty of putting the file name as the column header.
dinfo = dir('*.txt');
filenames = {dinfo.name};
outfile = 'collapse.xlsx';
colnames = 'A':'Z'; %needs fixing if you have more than 26
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, basename] = fileparts(thisfile);
S = fileread(thisfile);
CM = regexp(S, '^(COLLAPSE MULTIPLIER =\s+)(?<CM>\S+)', 'names', 'lineanchors');
collapses = [basename, num2cell(str2double({CM.CM}))].';
writecell(collapses, outfile, 'range', colnames(K) + "1");
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!