20130402_Programmer_defined_functions_(2)

Report
Add to your agenda!
• Discovery Day. Wednesday, April 3rd – all day
– Poster Presentation (Cafeteria) & Slide presentations (COA Atrium). For
example:
•
•
•
•
“Formula Hybrid High Voltage System”
“Development of Next Generation IPMC Actuator for Flapping Wing UAV”
“Mt. Everest Skydiving Research”
“Tolerance for Ambiguity of Air Traffic Management Students”
• TOMORROW! Wednesday, April 3rd, 7PM, IC Auditorium
– “Innovation: Risk are necessary!”
• COE Forum. Monday, April 8th, 6PM, Lehman Atrium
Dr. Mote
– free food!
– Each college has representatives on the SGA and they host a forum for
their college. Answer questions, give opinions!
• Arts & Science, Aviation. Business, Engineering
1
Programmer-Defined
Functions (2)
1.
2.
3.
4.
Basics & Advantages
Vocabulary & Order MATLAB follows
Review of the general syntax (i.e. setup)
Full example
2
1. BASICS
• A function is nothing but a “keyword”
– If that keyword is MATLAB’s, it is a “built-in” function
– If the programmer (you) created the .m file associated with the
keyword, it is a “programmer-defined” function
3
1. Review of advantages
1.
2.
Focus
Independence (they can be tested separately)
3.
4.
5.
6.
7.
Memory efficiency
Easier to debug the overall code
Clarity
Re-usability
Modularity
4
1. “dis”-advantage
Only 1 disadvantage:
a lot more .m files within your directory
5
2. Review of vocabulary
•
Main script file: The script file that contains the original overall project.
•
Function definition: the function header and the actual lines of code the function
Function
has to execute. (a little file for each new keyword)
definition
•
Function call: the command that calls upon the execution of the code that is inside
the function definition
– Usually placed within the main script file, but can also be within another
function definition. (A function CAN call another function you made!)
•
Passing arguments: giving inputs to the function definition.
•
Return info: final variables that the function definition calculated and gives back
Main
script file
Function call, pass arguments
Return info
•
Collection variables: The variables which receive the return info
Variable = …
6
Vocabulary is important!
Professors & tutors will use these words! (instead of the “thingy”)
In a ain script OR
another function file OR
the command window!
“Function call”
Passed “arguments” (2)
Collecting the
“Return-value”
“Function parameters” (5)
“Return-info”
50 lines of “documentation”
444 lines of code!
“Function file”
7
Common mistake
• The “function call” is NOT the “function’s name”
Here are 3
examples of
“function calls”
This file is the
“function
definition”
The name of the function is
changeToLetter()
regardless!!!!!
8
Order MATLAB follows to communicate
Collect the
returned value
“Return” the info
Pass the arguments.
Replaces the parameters.
Assign a value to
the return-info
Answer = ….
Execute 444 lines of code!
9
3. General Syntax
• In a separate .m file:
1. function <return info> = <function name>(<parameters>)
2. % <documentation/help>
..
% <author etc..>
<function body>
• Variables used in return-info, parameters, collecting return
values, and arguments can be:
 Hardcoded (example: 32)
 Variables (example: age)





Numerical values
Strings
Numerical Arrays
Cell-Arrays
… anything!
10
Example: askForValidate.m
• Re-usability
Any questions overall?
11
8. Application: Shortening Codes!
Travelling Delays? Who/What’s to blame???
http://www.transtats.bts.gov/OT_Delay/OT_DelayCause1.asp?pn=1
12
8. Application: Shortening Codes!
• Excel sheets. Arriving Flights/Delayed Flights/Reasons
..
2010
2011
2012
2013
13
21 columns.. Only a few useful today
1, 2 Year and month
3, 4 Carrier and its name
5, 6 Airport, and its name
7 arr_flights
Count of flights
8 arr_del15
Count of flights delayed (>15minutes)
9 carrier_ct
10 weather_ct
11 nas_ct
12 security_ct
13
14
15
16
17
18
19
20
21
late_aircraft_ct
arr_cancelled
arr_diverted
arr_delay
carrier_delay
weather_delay
nas_delay
security_delay
late_aircraft_delay
Late due to carrier (maintenance or crew problems, aircraft cleaning, baggage loading,
fueling, etc)
Late due to weather (Significant meteorological conditions )
Late due to National Aviation Delay System (non-extreme weather conditions, airport
operations, heavy traffic volume, and air traffic control)
Late due to evacuation of a terminal or concourse, re-boarding of aircraft because of
security breach, inoperative screening equipment and/or long lines in excess of 29
minutes at screening areas
A previous flight with same aircraft arrived late, causing the present flight to depart
late.
Count of aircraft cancelled
Count of aircrafts diverted
14
Solve for..
1. Percentage of flights delayed
1.
2.
3.
4.
5.
Percentage of those flights delayed due to carrier’s fault
Percentage of those flights delayed due to weather
Percentage of those flights delayed due to NAS
Percentage of those flights delayed due to security
Percentage of those flights delayed due to late flights
2. Percentage of flights arrived on time
ℎ − (ℎ + ℎ + ℎ )
ℎ
∗ 100
3. Present result in table
4. If time, present results in pie chart
15
2 Functions to create
[nbs, headings]=extractCorrectData(fileChosen);
Depending on the file chosen (2010? 2009?...), get rid of all the
lines that have blanks, and only return the nbs and the headings
needed.
flights_on_time = analyze(headings, nbs);
Given the headings and numbers needed, calculate all
percentages and display in a table. Calculate and return the % of
flights on time.
16
extractCorrectData.m
function [nbs, headings]=extractCorrectData(fileChosen)
%USE AS: [nbs, headings]=extractCorrectData(fileChosen);
% Depending on the file chosen (2010? 2009?...), get rid
% of all the lines that have blanks, and only return the
% nbs and the headings needed.
% by <author goes here>
17
extractCorrectData.m
function on_time = analyze(headings, nbs)
%USE AS: on_time = analyze(headings, nbs);
% Given the headings and numbers needed, calculate all percentages and
% display in a table. Calculate and return the % of flights on time.
% by <author would go here>
18
Main file
%Airline On-Time Statistics and Delay Causes
%by Caroline
%http://www.transtats.bts.gov/OT_Delay/OT_DelayCause1.asp?pn=1
clc
clear
repeat = 1; %force loop to start
while repeat ==1
clc
%let user pick file
myFile = uigetfile('*.*');
%get rid of bad data, and useless columns
%calculate percentages/display table
%show year and flight on time
year =
fprintf('Flights were %.2f%% on time in %s.\n',flights_on_time,year)
%ask user to repeat
repeat = input('Choose another file? (1 for yes, anything else quits): ');
end
19
Online App shows ALL data
• I just wanted to focus on the “not on time”
20
Quick reminder
• sum(matrix) sums each COLUMN.
x =
17
19
3
19
13
2
6
11
20
20
4
20
34
37
44
>> sum(x)
ans =
39
21
Improvements?
• Note how LONG it takes for MATLAB to process excel sheets.
• How about combining ALL years into 1 xls sheet?
>> memory issues
• How about putting each year into a separate sheet?
• Giving the user the possibility to analyze only 1 airline?
22

similar documents