Searching multiple Strings in file

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

 Respuesta aceptada

Guillaume
Guillaume el 7 de Ag. de 2018
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 comentarios

Robert Worm
Robert Worm el 7 de Ag. de 2018
Editada: Robert Worm el 7 de Ag. de 2018
Thanks for your help. This does the job.
Alternatively multiple expressions are added with or: '|'.
Regexp at first appears to be very convenient but also not intuitive.
Stephen23
Stephen23 el 7 de Ag. de 2018
Editada: Stephen23 el 7 de Ag. de 2018
"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 el 7 de Ag. de 2018
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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Ag. de 2018

Comentada:

el 7 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by