Sscanf /sprintf usage

7 次查看(过去 30 天)
Eric
Eric 2012-5-11
Hello,
So, apparently I don't understand very basic I/O....can somebody help me?
if num=343.1; str=sprintf('%10.3f',num); numdata=sscanf(str,'%10.3f'); why does numdata ~= num ???
Now, I can make it work by simply saying: numdata=sscanf(str,'%f'); then numdata=num;
But, I would like to use the former formatted sscanf to check the formatting of an input file...

采纳的回答

Sarah Wait Zaranek
Sarah Wait Zaranek 2012-5-11
I think this is your issue --
From doc for sscan - "Format specifiers for the reading functions sscanf and fscanf differ from the formats for the writing functions sprintf and fprintf. The reading functions do not support a precision field. The width field specifies a minimum for writing but a maximum for reading"
This code would work:
num=343.1;
str=sprintf('%10.3f',num);
numdata=sscanf(str,'%f');

更多回答(1 个)

Walter Roberson
Walter Roberson 2012-5-11

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by