Picking out stage coordinates using sscanf

9 Ansichten (letzte 30 Tage)
Jason
Jason am 12 Okt. 2015
Bearbeitet: Stephen23 am 12 Okt. 2015
Hi, I have a string where X&Y stage coordiantes are concatenated together with a comment.
-1042,7953**Tile1
so X=-1042, Y=7953, Comment=Tile1
I've trying to use sscanf to decompose these back to X, Y and comment:
num=sscanf(txtline, '%f,%f%s')
However its not appearing to give me what I need. Perhaps the occurance of the minus sign is messing things up. The problem is, is that either of the numbers can be positive or negative (positive appears with no prefix, negative appears with a '-').
Thanks for any advice

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Okt. 2015
Bearbeitet: Stephen23 am 12 Okt. 2015
>> A = sscanf('-1042,7953**Tile1','%f,%f**%s');
>> X = A(1)
X = -1042
>> Y = A(2)
Y = 7953
>> C = char(A(3:end).')
C = Tile1
The trick is to read the sscanf documentation, and in particular this line: "If the format includes: ... A combination of numeric and character specifiers, A is numeric, of class double. MATLAB converts each character to its numeric equivalent."
This mans the first two values are the numeric values in your string, and the rest are the character values. We simply convert these values back to character to get the comment string.
The negative sign - or a positive sign + does not cause any problems for sscanf, because the format %f recognizes both of these signs.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by