specific text to excel

4 Ansichten (letzte 30 Tage)
Bishal Chudal
Bishal Chudal am 18 Aug. 2021
Kommentiert: Walter Roberson am 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.

Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentare
Bishal Chudal
Bishal Chudal am 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 am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by