Toggle between temperature units for unit conversion

조회 수: 1 (최근 30일)
Fazlollah
Fazlollah 2023년 2월 14일
댓글: Image Analyst 2023년 2월 23일
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
Fangjun Jiang
Fangjun Jiang 2023년 2월 14일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 2월 17일
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
Jon
Jon 2023년 2월 17일
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.
Fazlollah
Fazlollah 2023년 2월 23일
편집: Fazlollah 2023년 2월 23일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jon
Jon 2023년 2월 14일
편집: Jon 2023년 2월 14일
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
Fazlollah
Fazlollah 2023년 2월 23일
편집: Fazlollah 2023년 2월 23일
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?
Image Analyst
Image Analyst 2023년 2월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by