Function with two outputs and multiple references

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

Chriss
Chriss on 23 Apr 2017
Edited: Chriss on 23 Apr 2017
Sorry you are right. I am not sure how to actually do it. It is part of an assignment so not real data.
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.
Chriss
Chriss on 23 Apr 2017
Edited: Chriss on 23 Apr 2017
The xls files are of course imported as i have used the data in other asssignments. As input i want my destinations - but can i refer to the imported file in the header or do i have to write all airports manually. I can't get it to accept either.

Sign in to comment.

Answers (1)

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

This is what i've made so far. i've managed to get it to return the airport name, but i can't get it to return the weekly passengers.
function [airportName, passengersPerWeek]=airportStatistics(destination)
global Destinations Departures;
airportName=0;
passengersPerWeek=0;
for i=2:size(Destinations, 1)
if strcmp(Destinations{i,1},destination)
airportName = Destinations{i,2};
end
end
for i=2:size(Departures,3)
if Departures{i,3} == Destinations{i,2}
passengersPerWeek = (countcats(Departures)*7)+(countcats(Departures)*0.6);
end
end
end
The reason i count the categories times 7 and then multiply by 6 is, that there is 20 % more departures friday-sunday.
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.
Came to the same conclusion. i rewrote the last part as:
for i=2:size(Departures,3)
if strcmp(Departures{i,3},destination)
passengersPerWeek = sum(ismember(Departures(2:size(Departures,1),3),destination));
end
end
but it still won't post the departures. can you se a fault in the above?
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.

Sign in to comment.

Asked:

on 22 Apr 2017

Commented:

on 24 Apr 2017

Community Treasure Hunt

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

Start Hunting!