- 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.
convert celsius to fahrenheit with a popupmenu
4 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Adam Danz
2019-9-6
编辑:Adam Danz
2019-9-6
The callback function to the "calculate" button should do the following,
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!