fprintf is showing NaN
조회 수: 14 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!