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

조회 수: 15 (최근 30일)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2023년 1월 23일
답변: dpb 2023년 1월 23일
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

답변 (1개)

dpb
dpb 2023년 1월 23일
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.

카테고리

Help CenterFile Exchange에서 Text Analytics Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by