Crosswind Component - App Designer (If Else Statement Help)

6 views (last 30 days)
Hello All,
I'm currently trying to apply a small program that I made to a GUI. The program I created uses the basic math involved in calculating the best runway for an airplane to land on based on the wind direction, wind speed, available runways, and max allowable crosswind component.
Wind direction = 360 degrees, Wind speed = 20 kts, Available Runways = 16, 29, 32 (AKA 160 deg, 290 deg, 320 deg), Max crosswind component = 13 kts.
Based on the given values, one would choose to land on runway 32. My program itself chooses the correct runway, but when I transpose its code into the App Designer, it decides that there is no good option for landing. As it stands right now, I have the App Designer code setup to run a Callback function once the "calculate" button is pressed. If anyone could provide an answer to my problem, it would be very appreciated. I have attached the program file an image of my designer view.
Code:
function ButtonPushed(app, event)
% Assigns input values to variables
pwdir = app.wdir.Value;
pwspeed = app.wspeed.Value;
prunways = str2num(app.runways.Value);
pcross = app.cross.Value;
%Preallocates array for calculated crosswind components
comps = [100 100 100 100 100 100 100 100 ];
% Reads the number of runways
number = numel(prunways);
for i = 1:number
%Finds current runway and puts it into degrees
current = prunways(i);
current = current * 10;
%Finds the difference between the current runway and wind direction
diff = abs(current - pwdir);
mult = sind(diff);
%Finds crosswind component
xwind = abs(pwspeed * mult);
%Stores crosswing component into an array
comps(i) = [ xwind ];
end
% Finds the lowest crosswind component and its location in the array
[best, location] = min(comps);
% Determines if maximum crosswind will be reached
if best <= pcross
chosen = prunways(location);
app.output.Value = chosen;
else
best > pcross;
app.alert.Value = 'Landing is not advised. Please find a new airport.';
end
end
end
*Note: The "Available Runways" is a text field due to the fact that I could not figure out how to input an array into a numeric field. I used the string to number function to turn the inputted runways into an array.
%
  1 Comment
Adam
Adam on 30 Aug 2017
Put breakpoints in at key points and step through the code. It is by far the easiest way to solve a large number of problems that get asked. Staring at code and hoping to spot where it might go wrong with given inputs is far more time-consuming than just looking at the variables directly.
As an aside, if you had your code working outside of AppDesigner you should just call that function from your AppDesigner code with the minimal code at the GUI end. Even if you never want to run your program just from command line in future it is always good to think that way from a design perspective, to keep your main program in its own function where you just pass in the parameters that could come either from UI or from a script or command line or anywhere. Pasting all the code into the GUI makes it very inflexible and muddies the waters when it comes to working out if a bug is at the UI end or in the algorithm.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!