If & elseif to determine temperature essentially
3 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I'm new to matlab and am trying to learn how to write a script that will take an input variable and look for a letter (K, C, F) and do something if it sees one of these 3 letters. The script below is specific to only temperatures in kelvin but I want to make an if else condition that I can use so it will either convert to kelvin and then do the math or not do it at all. The converison for kelvin is K = 273.15 + C & and the conversion of fareinheit to celsius is C=(F-32)*(5/9).
Here is my script
T_C = input('The absolute cold temperature is: '); %asks user for Absolute Cold Temp
T_H = input('The absolute hot temperature is: '); %asks user for absolute hot temp
n = 1-T_C/T_H; %calculates Carnot efficieny
fprintf('The Carnot efficieny is %.3f\n', n) %displays Carnot efficieny
0 个评论
回答(1 个)
William Rose
2024-6-17
编辑:William Rose
2024-6-17
Read the help for "if... elseif...else...end" and the help for "switch...case" to learn about different ways to approach your problem.
Your code gives an error if the user answers "273 K" or "0 C" or "32 F".
That is because "273 K" is not a numeric expression. Use "s" option to read the input as a text string. If you read the input as a string, then you must extract the letter part of the string (if any) and the numeric part separately, and take appropriate action. Consider whether the code should work if the user leaves out the space between the number and the letter.
3 个评论
Sam Chak
2024-6-17
移动:Sam Chak
2024-6-17
Hi @Zachary
The following code allows you to extract both the numeric value and the character representation of the temperature. The temp variable holds the temperature degree, while the scale variable indicates the temperature scale - 'C' for Celsius, 'F' for Fahrenheit, and 'K' for Kelvin.
With the scale information, you can use a switch-case-otherwise construct (as suggested by @William Rose) to determine the appropriate temperature conversion formula to apply.
Update the code and let us know if you need further assistance.
%% Demo
str = '36.9 C'; % to be replaced by user 'input()' command
temp = str2double(regexp(str, '\d+(\.\d+)?', 'match'))
scale = extractAfter(str, strlength(str) - 1)
William Rose
2024-6-17
Suppose you have
txtC = input('The cold temperature is: ',"s");
and the user may answer "273" or "273 K" or "0C" or "0 C" or "32F", etc. Then txtC is a text string which may or may not have a letter at the end.
T_C = str2double(extract(txtC, digitsPattern));
The line above puts the numeric part of the input string ninto variable T_C.
scale=txtC(end);
The line above puts the last character of the input string into variable "scale". "scale" may be "F" or "C" or "K", or it will be a numeric digit, if the user entered a number only.
Use "switch" or "if" to take appropriate action, depending on the value of "scale". Use "otherwise" with "case", or use "else" with "if", to handle the possibility that "scale" is neither F nor C nor K.
Do likewise for the high temp.
另请参阅
类别
在 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!