specific text to excel

4 次查看(过去 30 天)
Bishal Chudal
Bishal Chudal 2021-8-18
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.

回答(1 个)

Walter Roberson
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 个评论
Bishal Chudal
Bishal Chudal 2021-8-19
I have attached an input files, please help me to go trough it. I want those 1000 data from each input file in an excel sheet. Thanks
Walter Roberson
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 CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by