Toggle between temperature units for unit conversion
Show older comments
Hi,
I am trying to make a simple temeprature unit conversion app in MATLAB app maker with a dropdown menu to toggle between temperature units: C, F, and K. I used switch case function to convert degree c to F or K; however, when I want to return back to c or toggle between the units the program gives wrong values. Please help me complete the code to be able to toggle between temperature units. Thanks
function DropDownValueChanged(app, event)
Value=app.TemperatureEditField.Value
switch app.DropDown.Value
case 'C'
app.TemperatureEditField.Value=Value;
case 'F'
app.TemperatureEditField.Value=Value*1.8+32;
case 'K'
app.TemperatureEditField.Value=Value+273.15;
end
end
1 Comment
Fangjun Jiang
on 14 Feb 2023
Of course it has issues. If you continue selecting "K", the program flow will continu adding 273.15 to the current value. You need to add something to recognize the current unit.
Accepted Answer
More Answers (1)
I think this does what you want.
I have fixed a few errors in your original code, I think you were confused about the value of the drop down selection and the value of the temperature, which was in the edit field.
Also I added another edit field for the resulting output in the new units. Otherwise, as mentioned by @Fangjun Jiang, with repeated operation the value stored in the original edit field no longer has units of C, so the conversion which you have which convert from C to F or K would no longer be correct. Easier to keep the original value as is and then just output to a different edit field.
11 Comments
Jon
on 15 Feb 2023
The code is in the attached app2.mlapp. You need to open the code with the editor rather than just running it. The editor should open if you double click on the app2.mlapp from the files listed in your current folder, within your MATLAB session. Make sure that the current folder is set to the location where you have downloaded the app2.mlapp file.
Exactly what errors do you get when you try to run the app. Please cut and paste them here.
Fazlollah
on 15 Feb 2023
Jon
on 15 Feb 2023
To use the code you type in the value of interest into the left hand edit box and then select the units you want to convert to with the drop down and the result is displayed in the right hand edit box.
When the code starts up I wanted to have some initial value in the left hand box, so I set it to 25deg C, that is the line you reference above
app.TemperatureCEditField.Value = 25;
which is in the start up function.
So yes, every time you restart the code (open the app) it will be set to 25 deg C, then you type a new value in that field and select the units you want to convert to.
Jon
on 15 Feb 2023
I have attached a slightly improved version which ensures that the output edit box is updated whenever the input edit box is changed. In this version you will see that there is now a TemperatureCEditFieldValueChanged function which in turn calls the DropDownValueChanged function to recompute the output values with the currently selected units.
Jon
on 17 Feb 2023
Did this answer your question?
Fazlollah
on 17 Feb 2023
Jon
on 17 Feb 2023
I was following the typical approach used for example by Google if you ask to change units e.g. Google search convert 25 c to f, you get two boxes, one with the original units the other with the new units.
Jon
on 20 Feb 2023
I was interested to see whether a more general matrix oriented approach for unit conversion could be used in place of the switch case structure in @Voss's very nice implementation.
To do this I implemented the attached function convt (which could also be included in the app, or if used more widely, left as a independent function). I then replaced the switch case logic with a single call to convt in the attached app (modified from @Voss's)
Image Analyst
on 23 Feb 2023
Not sure what you mean. Isn't it the obvious:
Tc = (Tf - 32) * 5/9;
Rho_H2O = 999.6 + 2.0438e-1 * Tc - 6.174e-2 * Tc ^ 1.5
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!