How do I parse this string?

3 Ansichten (letzte 30 Tage)
K E
K E am 30 Jan. 2013
I would like to parse the following string
a='4:256404:val=9.999e+20:val=1.82:val=2.15:val=2.66:val=2.18:val=1.81';
to obtain the vector x = [9.999e+20 1.82 2.15 2.66 2.18 1.81]. In reality the string is 5308 characters long and there are very many of them. I believe I can't parse this with regexp because I want to separate with a defined sequence ':val='. I could do the following, but is there a cleaner way?
matchThisString = ':val=';
iBreak = findstr(a, matchThisString);
x = NaN*ones(1, (length(iBreak)-1));
for ii = 1:(length(iBreak)-1)
iStart = iBreak(ii) + length(matchThisString);
iEnd = iBreak(ii + 1) - 1;
x(ii) = str2num(a(iStart:iEnd));
end

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 30 Jan. 2013
Bearbeitet: Azzi Abdelmalek am 30 Jan. 2013
a='9.999e+20:val=1.82:val=2.15:val=2.66:val=2.18:val=1.81'
b=str2double(regexp(a,':val=','split'))
  1 Kommentar
K E
K E am 30 Jan. 2013
Great! I thought I could not use regexp here because it was a specific sequence, but turns out I can!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by