String parsing
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuesta aceptada
Geoff
el 30 de Mayo de 2012
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 comentarios
Geoff
el 30 de Mayo de 2012
\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.
Más respuestas (0)
Ver también
Categorías
Más información sobre String Parsing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!