- The string "track-length keff = " is unique to the data line of interest in the file, and
- There is only one such line per file.
Pull numeric data from a mixed text data into a matrix in a loop
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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):
The above will require R2020b or later for both readlines and pattern.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!