필터 지우기
필터 지우기

convert celsius to fahrenheit with a popupmenu

조회 수: 2 (최근 30일)
Jhon Rackham
Jhon Rackham 2019년 9월 6일
댓글: Adam Danz 2019년 9월 9일
Hi guys, i need your help with something:
I need a program to convert celsius to fahrenheit
on my GUI i have 2 edittext and and 1 popupmenu and 3 buttoms
i have to insert 1 value on the edittext 1, select the value to convert with the popupmenu (in this case fahrenheit)
and when i do clic on the bottom calculate, the value in celsius should be print on the edittext 2.
I hope you guys can help me with the code.
jejeje.png

답변 (1개)

Adam Danz
Adam Danz 2019년 9월 6일
편집: Adam Danz 2019년 9월 6일
The callback function to the "calculate" button should do the following,
  • Get the string property in "temperature" field and convert the string to a number using str2double()
  • get the C/F selection from the dropdown menu
  • Use a switch-case or a conditional statement (if) to create actions for c->f and f->c conversions. Each section will produce a converted temperature.
  • Convert the converted temperature to a string using num2str()
  • Update the string property in the "result" field
  • Update the C or F symbol to the right of the Result field.
I'm not sure if you're using app designer, guide, or uicontrol so I can't be more specific but if you need help with any of those steps, feel free to leave comments.
Here's a template that needs adapted to your situation. Hopefully the variable names are self explanatory.
inputTemp = temperatureHandle.String; % input temperature string
inputTempNum = str2double(inputTemp); % input temperature number
selectedConversion = menuHandle.String(menuHandle.Value); % selected conversion cell-string
switch selectedConversion{1}
case 'Celcius' %must be an exact match to the menu string
% convert C to F
outputTemp = (inputTempNum x (9/5)) + 32;
outputSymbol = 'F';
case 'Fahrenheit' %must be an exact match to the menu string
% convert F to C
outputTemp = (inputTempNum - 32) x (5/9);
outputSymbol = 'C';
end
outputString = sprintf('%.2f',outputTemp); % output temperature rounded to 2dp
resultHandle.String = outputString; % assign the temperature output
outputSymbolHandle.String = outputSymbol; % assign the temperature output symbol
  댓글 수: 9
Jhon Rackham
Jhon Rackham 2019년 9월 8일
Thanks a lot, you save me. :)
Adam Danz
Adam Danz 2019년 9월 9일
@Jhon , I'd be glad to continue the discussion if needed (in response to your PM).

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by