Toggle between temperature units for unit conversion

2 views (last 30 days)
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)
switch app.DropDown.Value
case 'C'
case 'F'
case 'K'
  1 Comment
Fangjun Jiang
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.

Sign in to comment.

Accepted Answer

Voss on 17 Feb 2023
You need to take into account what the old unit was before it was changed. Fortunately, the ValueChangedFcn of a uidropdown provides an event structure which contains that information. See the attached app.
Fazlollah on 23 Feb 2023
Edited: Fazlollah on 23 Feb 2023
Thank you Voss, this is working great.
With this conversion, I have another question. Assume I have an equation for density of water as a function of temperature in degree C. For example,
If my temperature input is in F, then how can I tell the program to put the temperatuer in degree C into the density equation? Can you help me with this one too?

Sign in to comment.

More Answers (1)

Jon on 14 Feb 2023
Edited: Jon on 14 Feb 2023
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.
Image Analyst
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

Sign in to comment.


Find more on Introduction to Installation and Licensing 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!