behavior of for loop with strings of chars ?

25 visualizaciones (últimos 30 días)
genevois pierre
genevois pierre el 30 de Oct. de 2018
Respondida: TADA el 1 de Nov. de 2018
Hi, this for statement
for string = ['=A=', '=B=', '=C=']
[starts, ends] = regexp(line, string);
the strings read as single '=' character in regexp ; as = is not a special character according to regexp, there is no need to precede it with \, as in '\=A\=' ? I tried some solutions using additional variables to represent those strings, or sprintf, but it didn't work ... Can somebody help me ? Thank you
  8 comentarios
TADA
TADA el 1 de Nov. de 2018
Editada: TADA el 1 de Nov. de 2018
there should be an accept answer somewhere
Come to think of it, I don't know exactly how you build your pattern there and if it really is a simple pattern as you wrote here, but a single pattern can easily cope with all three letters
regexp(line, '=[ABC]=')
Guillaume
Guillaume el 1 de Nov. de 2018
The best thing would be for TADA to write his answer as an actual answer so that pierre can accept it. You can't accept comments.
The question should not be closed as it would no longer be visible. It contains useful information.

Iniciar sesión para comentar.

Respuesta aceptada

TADA
TADA el 1 de Nov. de 2018
you will probably want to switch from a vector to a cell array of character vectors
patterns = {'=A=' '=B=' '=C='};
for i = 1:length(patterns)
[starts, ends] = regexp(line, patterns{i});
end
or better yet like Stephen Cobeldick mentioned, send the cell array over to regexp
patterns = {'=A=' '=B=' '=C='};
[starts, ends] = regexp(line, patterns);
or just put it all in a single pattern
[starts, ends] = regexp(line, '=[ABC]=');

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by