What is the difference between %f and %s ?

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

%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,

Sign in to comment.

Answers (1)

madhan ravi
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

Thanks man. I understand it now
madhan ravi
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.

Sign in to comment.

Tags

Asked:

on 29 Oct 2018

Edited:

on 29 Oct 2018

Community Treasure Hunt

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

Start Hunting!