Picking out stage coordinates using sscanf

1 view (last 30 days)
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

Accepted Answer

Stephen23
Stephen23 on 12 Oct 2015
Edited: Stephen23 on 12 Oct 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.

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!