Function with two outputs and multiple references

10 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2017-4-23
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 2017-4-23
编辑:Chriss 2017-4-23
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.

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2017-4-23
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 个评论
Chriss
Chriss 2017-4-23
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?
Image Analyst
Image Analyst 2017-4-24
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by