Handling unit conversion in a simple App Designer application

13 Ansichten (letzte 30 Tage)
Lucas Nogueira
Lucas Nogueira am 14 Dez. 2021
Beantwortet: Geoff Hayes am 17 Dez. 2021
Hello.
Earlier this month, I began using MATLAB's App Designer to develop a simple aircraft cruising performance calculator. Things went smoothly at first, as I managed to build an application that handles performance calculations in SI units with perfection. Now, my self-prescribed challenge is to enable users to swap units - for example, use km/h for cruise airspeed instead of m/s, or replace wing loading with lb/ft² in lieu of N/m². I've included a bunch of state buttons and radio buttons to implement this feature, as indicated in red:
However, I'm having serious trouble on how to handle unit selection in the app backend. One example: I've set up four possible units for initial cruise airspeed, namely 'm/s', 'km/h', 'mi/h', and 'knots' (see above). My idea to handle unit conversion of this kind was to set up four radio buttons, with 'm/s' as the default one, and then create a callback that multiplies the input cruise airspeed (app.CruiseAirspeed) by the conversion factor of 'm/s' to the unit of the selected radio button, as shown below. My intention is to have the code convert properties from any selected non-SI unit ('km/h', 'mi/h', 'knots') back to SI units, so that I can use the SI-unit based formulas when the user hits 'Calculate'.
switch app.AirspeedUnitsButton.SelectedObject;
case app.meterspersecondButton
app.cruiseV = app.CruiseAirspeed.Value;
app.AirspeedLabel.Text = 'm/s';
case app.kmhButton
app.cruiseV = app.CruiseAirspeed.Value*0.2778;
app.AirspeedLabel.Text = 'km/h';
case app.mihButton
app.cruiseV = app.CruiseAirspeed.Value*0.4470;
app.AirspeedLabel.Text = 'mi/h';
case app.knotsButton
app.cruiseV = app.CruiseAirspeed.Value*1.944;
app.AirspeedLabel.Text = 'knots';
end
The code above works if I change from the default unit (m/s) to one of the other three units. However, it yields wrong unit conversions if the user switches between any of the other three units - for example, from 'knots' to 'km/h' or from 'knots' to 'mi/h' - because the conversion factor used in the switch statement only applies if the starting unit was meters per second, and converting knots to km/h or knots to mi/h obviously requires different conversion factors. I'm sure there's a better way to proceed here, so I'd appreciate some help from more experienced users.

Antworten (1)

Geoff Hayes
Geoff Hayes am 17 Dez. 2021
@Lucas Nogueira - I think that you would need to keep track of (in addition to the current cruise airspeed), you would need to keep track of the units for that airspeed. Then, in the code that contains the above code, you would then do something like
conversionToMPerSecMap = containers.Map({'m/s','knots','km/h','mi/h'},[1,1/1.944,1/0.2778,1/0.4470])
currentCruiseSpeed = app.CruiseAirspeed.Value;
currentCruiseSpeedUnits = App.AirspeedLabel.Text;
currentCruiseSpeedMPerSec = currentCruiseSpeed * conversionToMPerSecMap(currentCruiseSpeedUnits);
switch app.AirspeedUnitsButton.SelectedObject;
case app.meterspersecondButton
app.cruiseV = currentCruiseSpeed;
app.AirspeedLabel.Text = 'm/s';
case app.kmhButton
app.cruiseV = currentCruiseSpeed*0.2778;
app.AirspeedLabel.Text = 'km/h';
case app.mihButton
app.cruiseV = currentCruiseSpeed*0.4470;
app.AirspeedLabel.Text = 'mi/h';
case app.knotsButton
app.cruiseV = currentCruiseSpeed*1.944;
app.AirspeedLabel.Text = 'knots';
end
The above code uses a map to manage the units as a string (key) to provide the conversion factor (value). This assumes that the app.AirspeedLabel.Text is valid and that the strings written here are valid keys within the map.

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by