Filter löschen
Filter löschen

Extracting a number from a changing string using sscanf

2 Ansichten (letzte 30 Tage)
Hi there!
I'm having trouble extracting a number from a data string that isn't always consistent.
I'm getting incoming strings that sometimes change their format, but will always have a number with a "G" after it. For example:
ans =
+1.4105 G 799 ON 1X OFF
ans =
isj34 ME102+03994 G
ans =
ME101-04230 G 17 OFF
It can be formatted a whole mess of ways, but the one thing that is consistent is the sign, number, and "G" after it. (there are blank spaces sometimes, the ME10# will sometimes be there, sometimes will not, and if it is, there's no guarantee that the # is 1 or 2)
I've tried using sscanf to simply extract the number, for example:
sscanf('silj 90 ME102-014830 G','ME102%f G',1)
or
sscanf('silj 90 ME102-014830 G','%*s ME102%f G',1)
or
sscanf('silj 90 ME102-014830 G','%*f ME102%f G',1)
and I'm looking for:
ans =
-14830
So far I've only gotten blank strings.
Open to any suggestions. Thanks!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Mai 2016
sscanf('silj 90 ME102-014830 G','%*[^-+]%f G',1)
  1 Kommentar
Rodmehr Semnani
Rodmehr Semnani am 6 Mai 2016
I found a way to do it, but it looks like it's meant to be shown to CS majors being interrogated at Guantanimo. Your's is tremendously better and cleaner.
Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by