Reputation: 1
I have a string in matlab that looks like this:
str =
Z 1 -355.66338432 1
Z 2 1.38339828 412
Z 3 9.00000000 412
Z 4 -10.27835665 312 22 - 1
Z 5 3.00000000 612 2 2
Z 6 6.53259554 612 2 2
Z 7 5.00000000 812 33 - 2
Z 8 0.19040409 812 33 - 2
Z 9 2.00000000 812 3 3
Z 10 -1.00534284 812 3 3
Z 11 7.27727717 512 64 - 62 1
I want to extract the numbers in the third column but I can't seem to figure out the correct usage of regexp.
Upvotes: 0
Views: 1418
Reputation: 12486
I don't have MATLAB handy, so I can't provide code. But generally: Step through the string one line at a time, and use the regex:
^\s*Z\s*\d+\s*(-?\d+\.\d+).*$
This will capture the number as the first capture group, \1
. The regex expands as:
^ Start of line
\s*Z\s* First column - whitespace, literal `Z`, whitespace
\d+\s*a Second column - integer number, whitespace
(-?\d+\.\d+) Third column - a (possibly negative) decimal number.
.* Anything
$ End of line
See it work on Regexr.
Alternately, use the tool that's actually meant for this job: textscan()
. (Mathworks documentation.)
Upvotes: 1