fprintf- I dont want to print one result.

This is my code. I want that when the person put the value 0, the message print ONLY "Negative,has not being infected". But instance it give also the hour of being infected.
x= input ('Enter x: '); %Degree of reaction to the virus from a blood test.
if x==0
disp ('Negative, has not being infected.')
N=0;
elseif x<=11
N= 3*x + 36;
elseif x > 11 && x<=33
N= x*x - 10;
elseif x > 33 && x<=64
N= x-6;
end
fprintf('\n%g hours of being infected.\n',N)

 Accepted Answer

clear
clc
x= input ('Enter x: '); %Degree of reaction to the virus from a blood test.
showHours = 1;
if x==0
disp ('Negative, has not being infected.')
N=0;
showHours = 0;
elseif x<=11
N= 3*x + 36;
elseif x > 11 && x<=33
N= x*x - 10;
elseif x > 33 && x<=64
N= x-6;
end
if showHours
fprintf('\n%g hours of being infected.\n',N)
end

5 Comments

In the above construction there's no need for the showHours variable...simply
x= input ('Enter x: '); %Degree of reaction to the virus from a blood test.
if x==0
disp ('Negative, has not being infected.')
N=0;
elseif x<=11
N= 3*x + 36;
elseif x > 11 && x<=33
N= x*x - 10;
elseif x > 33 && x<=64
N= x-6;
end
if x, fprintf('\n%g hours of being infected.\n',N), end
There really should be another else clause for x outside the range provided for or the input parsing take care of that...but that Q? wasn't asked.
So an "if x" condition will always give "1" as a result, providing that x is different from 0? I did not know that, thanks for the rectification :)
dpb
dpb on 15 Dec 2019
Edited: dpb on 15 Dec 2019
IF doesn't "give" a result, it just performs the test of the expression. x is a variable in the above code no different than the one you created and its value is the value of the expression to be tested.
See the definition of logical TRUE in MATLAB syntax at the documentation for if that says
"if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric)."
Logical variables of T/F are 1/0, that is true, but the actual definition of true for logical tests is ~0/0. The idiom of if x==1 or if x==0 is completely superfluous in writing the explicit test.
Also note as it trips up most newbies is the phrase "and contains only nonzero elements". When testing array-valued expressions, all or any is needed to convert to a single logical result. You'll find many questions here on Answers that begin with "my expression fails to be true" owing to that oversight.
For one-liners like this my personal preference is the one-line expression rather than the full-blown indented construct. Don't let the formatting confuse you that there's anything different syntactically, though.
wow, thanks for the detailed explanation!
Another way to recast...
x= input ('Enter x: '); %Degree of reaction to the virus from a blood test.
if x<=0
disp ('Test negative, subject has not been infected.')
N=0;
return
end
% set polynomial coefficeints by regime
if x<=11, b=[0 3 36]; end % 3t+36
elseif x>11 & x<=33, b=[1 0 -10]; end % t^2-10
elseif x>33 & x<=64, b=[0 1 -6]; end % t-6
else error("Reaction >64 beyond correlation range"), end
N=polyval(b,x); % evaluate poly for x
fprintf('\nSubject %g hours of being infected.\n',N)
But, there's something funky in the correlation as given--it's grossly discontinuous...

Sign in to comment.

More Answers (1)

dpb
dpb on 15 Dec 2019
A number of ways...one would be to include the fprintf statement in an if....end clause itself on value of N (or x).
Another would be to exit inside the x==0 clause since there's nothing else to be done.
Yet another would be to place all the elseif clauses inside an else on what would then become the outer if...else...end construct.
It's unfortunate TMW didn't implement range expressions in switch; it would be more elegant imo...

Categories

Find more on Programming in Help Center and File Exchange

Asked:

Gyp
on 15 Dec 2019

Edited:

dpb
on 15 Dec 2019

Community Treasure Hunt

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

Start Hunting!