specific text to excel

조회 수: 4 (최근 30일)
Bishal Chudal
Bishal Chudal 2021년 8월 18일
댓글: Walter Roberson 2021년 8월 19일
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