Pull numeric data from a mixed text data into a matrix in a loop

1 visualizzazione (ultimi 30 giorni)
Hi,
I have hundreds of mixed text files and I would like to pull some of the numeric data and create a matrix using a for loop (In this example there is only two text file). In the attachment I only provided very small section of the data. There are ~3000 lines above and maybe ~1000 lines below, but the information I need is in this block.
I would like to pull the keff value and the deviation values and create a matrix. The information is not nesessarily located in certain line numbers, for each file it could be in different line. The string "keff" could be used in the file somewhere else in the file so I am not sure if I can use keff only as a pattern recogniztion.
at the end I would like to have a matrix as follows
1.1516 0.00114 (text1.txt, line# 11)
1.1195 0.00214 (text2.txt, line# 10)
Any idea? Pattern recognition?
Thank you

Risposte (1)

dpb
dpb il 23 Gen 2023
d=dir('yourmatchingFileWildCardExpression*.txt'); % get the list of candidate files dir() struct
pat=digitsPattern(1)+"."+digitsPattern(4,5); % define the numeric format looking for
N=numel(d);
data=zeros(N,2);
for i=1:N % iterate over the files
f=readlines(d(i).name); % read the file as string array
l=f(contains(f,"track-length keff = ")); % get the line of interest
data(i,:)=str2double(extract(l,pat)).'; % and extract the number values
end
NOTA BENE: The above assumes two fundamental things (not enough info supplied to verify either):
  1. The string "track-length keff = " is unique to the data line of interest in the file, and
  2. There is only one such line per file.
The above will require R2020b or later for both readlines and pattern.

Categorie

Scopri di più su Text Analytics Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by