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'.
app.cruiseV = app.CruiseAirspeed.Value;
app.AirspeedLabel.Text = 'm/s';
app.cruiseV = app.CruiseAirspeed.Value*0.2778;
app.AirspeedLabel.Text = 'km/h';
app.cruiseV = app.CruiseAirspeed.Value*0.4470;
app.AirspeedLabel.Text = 'mi/h';
app.cruiseV = app.CruiseAirspeed.Value*1.944;
app.AirspeedLabel.Text = 'knots';
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.