MATLAB: Behavior of for loop with strings of chars

for loopMATLABregexp

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

Best Answer

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]=');