フィルターのクリア

String parsing

13 ビュー (過去 30 日間)
skyimager
skyimager 2012 年 5 月 30 日
input string s='Temp : 34, Altitude : 35, Pressure : 45' . I now want to store Temp, Alt and Pressure value in three different Strings. How to do the parsing in this case..??? Pls help.

採用された回答

Geoff
Geoff 2012 年 5 月 30 日
It kinda depends on how flexible you need to be. Are the tags ever in a different order? Is the format EXACTLY as given? Are the numbers always integers?
Something as simple as this could work:
sscanf(s, 'Temp : %f, Altitude : %f, Pressure : %f')
[EDIT] Oh, sorry, you said "strings":
regexp(s, ':\s*(\d+),.*:\s*(\d+),.*:\s*(\d+)', 'tokens')
  2 件のコメント
skyimager
skyimager 2012 年 5 月 30 日
Thanks alot Sir. The actual data format is tragya$567$67$678$789$tragya , the first set of no should be stored as pressure value then the next set as temperature and so on. The order never changes.
Also can u please explain me how to from regular expressions for regexp function ? I mean what does s* (\d+) mean..???
Geoff
Geoff 2012 年 5 月 30 日
\s* matches 0 or more whitespace characters. \d+ matches 1 or more digits (note that if you want to accept non-integers or negative values, you can probably get away with using \w+ which matches 1 or more non-whitespace characters) . The parentheses means I want to specifically extract the stuff inside. Read the help for regexp, or find regular expression tutorials on the internet. These things are not just confined to MatLab.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by