I have a user inputting different units for temperature. However, I only want the program to be able to accept Capital letters, so if they put a lowercase, it would tell them to input it again. What would be the code for this? I have something like this right now. The while part needs fixing.
Unit = input('Enter the units of the inputted temperature:','s'); while Unit == c Unit == k Unit == f Unit == r Unit = input('Please enter the unit with a Capital Letter:','s'); end

 채택된 답변

Image Analyst
Image Analyst 2012년 2월 12일

0 개 추천

That's not user friendly. It would be very annoying to your users and I recommend you DON'T DO THAT, especially since it's not necessary at all. Just convert what they input to upper case with the upper() function, like this:
% Set up the dialog box parameters
prompt = {'Enter temperature:','Enter the temperature scale:'};
dialogTitle = 'Enter parameters';
numberOfLines = 1;
defaultAnswers = {'100','C'};
userResponse = inputdlg(prompt, dialogTitle, numberOfLines, defaultAnswers);
% Extract out individual variables from the user response.
temperature = str2double(userResponse{1})
% Convert to upper case.
EoSI = upper(userResponse{2})

댓글 수: 4

Kevin
Kevin 2012년 2월 12일
That works but I should have made it a little bit clearer, sorry about that. I need it where the only units I want to be accepted to be C,F,K or R, and that the program must check to make sure that the unit entered is one of those.
Image Analyst
Image Analyst 2012년 2월 12일
Use a switch statement or an if/elseif/else block.
Image Analyst
Image Analyst 2012년 2월 12일
Possibly a better option is to use menu() to allow them to pick ONLY one of the allowed options.
Kevin
Kevin 2012년 2월 12일
I got the idea of using the if/elseif/else block but I'm just unsure of how to actually go about it

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2012년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by