Save numbers after text pattern in a vector

조회 수: 1 (최근 30일)
Roderick
Roderick 2024년 8월 2일
답변: Rik 2024년 8월 2일
Dear all
I have a text file that looks like these:
#------------------------------------------------------
# Atomistic spin configuration file for vampire v5+
#------------------------------------------------------
# Date: Wed Jul 31 19:19:54 2024
#------------------------------------------------------
Time: 1e-15
Field: 0 0 0
Temperature: 0.05
Magnetisation: -0.999883 0.0152572 -0.000478932
#------------------------------------------------------
Number of spin files: 1
spins-00000000.data
#------------------------------------------------------
I am interested in saving in an array the three numerical values after "Field:". I have tried something like
field_str=regexp(meta_file_read,'(?<=^TField:\s+)(\S+)\s*(\S+)\s*(\S+)','match','lineanchors'); % T
But the cell seems to be empty.
Any ideas?

답변 (1개)

Rik
Rik 2024년 8월 2일
Having a typo in your needlessly complicated regex doesn't help:
meta_file_read=["#------------------------------------------------------"
"Time: 1e-15"
"Field: 0 0 0"
"Temperature: 0.05"
"Magnetisation: -0.999883 0.0152572 -0.000478932"
"#------------------------------------------------------"];
[field_str,tokens]=regexp(meta_file_read,...
'^Field:\s+(\S+)\s*(\S+)\s*(\S+)',...
'match','tokens')
field_str = 6x1 cell array
{0x0 string } {0x0 string } {["Field: 0->0->0"]} {0x0 string } {0x0 string } {0x0 string }
tokens = 6x1 cell array
{0x0 string} {0x0 string} {1x1 cell } {0x0 string} {0x0 string} {0x0 string}
tokens{3}{1}
ans = 1x3 string array
"0" "0" "0"

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by