Searching multiple Strings in file

조회 수: 1 (최근 30일)
Robert Worm
Robert Worm 2018년 8월 7일
댓글: Guillaume 2018년 8월 7일
Hi,
The typical file format is as follows:
channelCfg 15 3 0
adcCfg 2 1
adcbufCfg -1 0 0 1 0
The text file is read. The keywords are adcCfg and adcbufCfg. Searching for the keywords only integers in that line have to be stored in a vector. I tried to get something to work with regexp and strfind but got no satisfying results.
Regards

채택된 답변

Guillaume
Guillaume 2018년 8월 7일
but got no satisfying results
And what is a statisfying result?
Is this what you're after?
filecontent = fileread('c:\somewhere\somefile.txt');
adclines = regexp(filecontent, '(?<=adc(buf)?Cfg).*', 'match', 'dotexceptnewline');
adcnumbers = cellfun(@str2double, regexp(adclines, '[+-]?\d+', 'match'), 'UniformOutput', false);
celldisp(adcnumbers)
  댓글 수: 3
Stephen23
Stephen23 2018년 8월 7일
편집: Stephen23 2018년 8월 7일
"Regexp at first appears to be very convenient but also not intuitive."
You will get used to them. It takes a lot of reading the documentation to use regular expressions effectively. This is your source of information:
Guillaume
Guillaume 2018년 8월 7일
not intuitive
Like any language, you have to learn it. It is very self-consistent and once you know it very intuitive.
Alternatively multiple expressions are added with or: '|'.
I'm not sure why you are mentioning this.
Is your question answered then?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by