# convert celsius to fahrenheit with a popupmenu

68 ビュー (過去 30 日間)
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.

サインインしてコメントする。

### 回答 (1 件)

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
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 件のコメント

Rik 2019 年 9 月 8 日
I already gave the complete instruction for this callback. If you have trouble with Matlab you should do a course like Onramp. If your issues are specific to GUIs I'll give you the callback here, and at the end of my comment I'll paste some tips to write your own GUI without having to use the confusing mess that is GUIDE.
So you will have to first retrieve the String property from the edittext,
InputValue=get(handles.edittext1,'String');
then convert that to a double (with str2double),
InputValue=str2double(InputValue);
use the switch you already have to find out what formula to use,
switch C2F_or_F2C
case 1
OutputValue=1.8*InputValue+32;
case 2
OutputValue=(InputValue-32)/1.8;
end
convert the result to a char array (use num2str or sprintf)
OutputValue=sprintf('%.1f',OutputValue)
and set the String property of your edit text.
set(handles.edittext1,'String',OutputValue)
My small guide to avoid GUIDE:
• Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
• Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
• When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
• You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
• Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
• More information about callbacks can be found in multiple places in the doc, for example here.
Jhon Rackham 2019 年 9 月 8 日
Thanks a lot, you save me. :)
Adam Danz 2019 年 9 月 9 日
@Jhon , I'd be glad to continue the discussion if needed (in response to your PM).

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by