specific text to excel

4 views (last 30 days)
Bishal Chudal
Bishal Chudal on 18 Aug 2021
Commented: Walter Roberson on 19 Aug 2021
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.

Answers (1)

Walter Roberson
Walter Roberson on 18 Aug 2021
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 Comments
Bishal Chudal
Bishal Chudal on 19 Aug 2021
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 on 19 Aug 2021
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

Sign in to comment.

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!