What is the difference between %f and %s ?
37 views (last 30 days)
Show older comments
I am working on if/elseif/else.
clear all, clc
x = input('Enter x = '); % ask user to type x
if x < 10 % check if x < 10
retval='less than 10';
elseif x<=20
retval='less than or equal to 20';
else
retval='greater than 20';
end
fprintf('x=%s\n',retval) %why %f does not work here. Why?
1 Comment
Dendy Gusmawan
on 29 Oct 2018
Edited: Dendy Gusmawan
on 29 Oct 2018
%f is used for output formated is numeric, more spesific its used for decimal number. %s is used to print output that formated as string. in your case i believe its because your output is formated as string so %f clearly doesnt work (since its not numeric)| %e scientific format, lower case e %E sientific format, upper case E %f decimal format, %s string, %i integer, %u following its type, %x hexadecimal lower case, %X hexadecimal upper case,
Answers (1)
madhan ravi
on 29 Oct 2018
Edited: madhan ravi
on 29 Oct 2018
%s represents character vector(containing letters) and %f represents fixed point notation(containining numbers). In your case you want to print letters so if you use %f formatspec you won’t get the desired result.
2 Comments
madhan ravi
on 29 Oct 2018
Edited: madhan ravi
on 29 Oct 2018
Anytime:), make sure to accept the answer if you got the answer to your question so that people know question is solved.
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!