Toggle between temperature units for unit conversion

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

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

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.

2 Comments

Very nice!
Gives the OP what he wanted with an interface with just one box. Good to know that the event structure returns the previous value.
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,
Rho_H2O=999.6+2.0438e-1*Tc-6.174e-2*Tc^1.5
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
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.

11 Comments

John, thank you for your answer. Could you please send me the code? you just attached the app. Also, the app gives me errors when I run it. thanks
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.
Thank Jon, I dont get the errors while loading the codes. However, I found this problem with the code that when I change the temperature to other values, e.g. 50C, code returns to the initial value of 25C. Actually the code just works for 25C and no other temperatures. Do you know how to fix this problem?
app.TemperatureCEditField.Value = 25;
I think the reason is line 65 of the code, which overwrites the input value.
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.
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.
Did this answer your question?
Hi John, this is not what I was trying to make. What is the point of having two temperatures. I was hoping to have just one edit field where I can change the temperature value and unit. Right now, your program has two boxes, one gets the input and one shows the output. My original code uploaded here just has one edit field. I dont want to have another one. Is it possible to do that?
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.
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)
Thank you Jon for the answer, yours also works perfectly. However, Voss answer is easier for me to understand.
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,
Rho_H2O=999.6+2.0438e-1*Tc-6.174e-2*Tc^1.5
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?
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.

Products

Release

R2020a

Asked:

on 14 Feb 2023

Commented:

on 23 Feb 2023

Community Treasure Hunt

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

Start Hunting!