fprintf is showing NaN

13 ビュー (過去 30 日間)
Olivia Warner
Olivia Warner 2021 年 2 月 5 日
回答済み: Raghav Gnanasambandam 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
Raghav Gnanasambandam 2021 年 2 月 5 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by