Function with two outputs and multiple references
Show older comments
I'm trying to create a function "airportStatistics" that takes a Destination (Airport 1, Airport 2...,Airport 20) as an input but i'm not sure how to go about it. The function must then return the name of the Destination (e.g. London) and the weekly number of passengers per destination. I have imported two excel-files containing the material. The first "Destination" contains the Destination in the first column and the name in the second:
'Destination' 'Airport Name'
'Airport 4' 'Hamburg International Airport'
'Airport 7' 'Sevilla International Airport'
'Airport 2' 'London Airport'
etc.
The second file contains the Destinations in the 3rd column one for each departure per day
'Destination'
'Airport 19'
'Airport 11'
'Airport 3'
'Airport 8'
'Airport 1'
'Airport 14'
etc.
In the function i must take into account that the destinations in the 2nd file only account for 1 day but the function has to return departures per week.
4 Comments
Walter Roberson
on 22 Apr 2017
John D'Errico
on 23 Apr 2017
How to do it? You start writing!
First, read in the xls file into MATLAB.
Start by writing a function header, that takes the necessary input, passed in as arguments.
Then look at the list of destinations. Count how many departures there are for each airport.
You won't get anywhere at all until you start making an effort. Then attack each part of the problem until you are done.
Answers (1)
Image Analyst
on 23 Apr 2017
I'd use readtable() to get the results in a table instead of a cell array. Then I'd probably use ismember() to find out what rows you need to deal with. The function line would look like
function [destinationAirport, numPassengersPerWeek] = airportStatistics(destinationAirport)
though I'm not sure if destinationAirport is just one airport or a list of a bunch of them. Also not sure why you're returning it from the function since you already have it, unless you're going to change it for some reason inside the function.
4 Comments
Chriss
on 23 Apr 2017
Image Analyst
on 23 Apr 2017
If it's not returning passengersPerWeek then you're never entering the "if" block where you assign it. Use the debugger to step through line by line and discover why it never gets in there.
I don't know what Departures and Destinations are, but if they're cell arrays of strings you're going to have to use strcmpi() instead of == to compare them.
Chriss
on 23 Apr 2017
Image Analyst
on 24 Apr 2017
I can't really figure all that out in my head. I suggest you use the debugger: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ just like I would do. I don't have your data so I can't do it, but you can do it just as well as I can.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!