convert celsius to fahrenheit with a popupmenu

5 ビュー (過去 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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by