fprintf is showing NaN

6 次查看(过去 30 天)
Olivia Warner
Olivia Warner 2021-2-5
i am tryng to create a prgram that gives a prognosis for a temperature reading in any units. the output always offers
The temperature of 36.667 degrees Celsius, results in a prognosis of NaN
why is my message reading NaN, and how can i fix it?
TEMP=input("Temperature in any Units");
UNITS=input("What are the Temperature Units? (Type only the number '1' for Celcius, '2' for farenheiht, or '3' for kelvin)");
if UNITS==1||2||3
if UNITS==1
T=TEMP;
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
if UNITS==2
T=(TEMP-32)*(5/9);
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
if UNITS==3
T=TEMP-273.15;
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
fprintf("The temperature of %.3f degrees Celsius, results in a prognosis of %.3f",T,PG)
end

回答(1 个)

Raghav Gnanasambandam
You need to add '%s' for printing a string in fprintf. Here is the corrected code.
TEMP=input("Temperature in any Units");
UNITS=input("What are the Temperature Units? (Type only the number '1' for Celcius, '2' for farenheiht, or '3' for kelvin)");
if UNITS==1||2||3
if UNITS==1
T=TEMP;
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
if UNITS==2
T=(TEMP-32)*(5/9);
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
if UNITS==3
T=TEMP-273.15;
if T<35
PG="Hypothermia";
elseif T>=35&&T<36.111
PG="Possibly Sick";
elseif T>=36.111&&T<37.5
PG="Normal";
elseif T>=37.5&&T<38.333
PG="Fever";
elseif T>=38.333&&T<40
PG="Severe Fever";
elseif T>=40
PG="Hyperplexia";
end
end
fprintf("The temperature of %.3f degrees Celsius, results in a prognosis of %s",T,PG)
end

类别

Help CenterFile Exchange 中查找有关 Biological and Health Sciences 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by