-1

I am taking a MATLAB programming class and we are currently working on a project which uses a database called Project2 of several structs of airline flight data (all 1 x N). One exercise requires us to create a function that identifies the number of flights segments (housed in the flights struct) that used the Boeing 737-800 aircraft. Below I have included the code for the function I created (NOTE: The format of the first line is such that was dictated in the instructions and must remain that way). Although this function seems to work and be free of bugs, it consistently returns a result of 0 and I cannot figure out why. Can anyone help? Suggestions for fixing the problem and/or cleaning up the code would be greatly appreciated!

function total = Problem2 (flights, aircraft, airlines, airports)
    load Project2
    id=findAircraftID (aircraft, Boeing 737-800)
    seg=0;
    for jj = 1:length(flights)
        if (strcmp (flights(1,jj).aircraft_id, id))
            seg=seg+1
        end
    end
    fprintf ('A total of %d flight segments used the Boeing 737-800 aircraft.\n', seg)
end

function id=findAircraftID (aircraft, AircraftName)
    id=0;
    for ii=1:length(aircraft)
        if (strcmp (aircraft(1,ii).name, AircraftName))
            id=ii;
            return;
        end
    end
end
arilaan
  • 384
  • 2
  • 17
Nea
  • 181
  • 2
  • 3
  • 10

2 Answers2

1

Why are you using strcmp to compare integers? Is aircraft_id a string? Perhaps you can cast id from an int to a string if so. Or better yet you can just use isequal(a,b):

if isequal(flights(1,jj).aircraft_id, id)
    seg=seg+1;
end

Also see other methods at Octave/MATLAB: How to compare structs for equality?

Also (or alternatively if that's not the issue) you're iterating through your second function and setting id several times, but only the last value goes into the first function. Take a closer look at your for loops to see whether you need to wrap them, store id as an array rather than a single integer, etc.

Never mind I see that your code could work if aircraft names are unique. strcmp should work in that case - but perhaps step through and check that you aren't having issues because of capitalization, spaces, etc.

Community
  • 1
  • 1
arilaan
  • 384
  • 2
  • 17
  • Thanks so much for your answer, arilaan! You were spot on about my misuse of strcmp instead of isequal. I had been reusing code from another exercise that compared input strings and names of airlines within another struct and completely forgot to account for the change in data type. I'm happy to say the function is working properly now :) As for your suggestion on the id variable, the last value is the one I'm looking for in this case, as each aircraft is given a unique ID and the second function need only return the one assigned to the AircraftName input argument. Again, thanks for your help! – Nea Nov 19 '13 at 04:48
0

Usually when you want to find an element in an array that matches a condition, you use something like

bWhenAis3 = B(A == 3);

To find the value of B when A is 3. This can return a vector of multiple values, and is usually much faster than an explicit loop.

In your code, aircraftID is an integer because it's returned by findAircraftID which returns ii. You cannot compare this with a string! You need to compare like types.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Floris- thanks for the response! I will definitely keep in mind your suggestion for the bWhenAis3 code and will experiment with it in the future. The class I'm taking is an intro to MATLAB, so we are still working with more basic functions like loops, but its always great to try out more practical/advanced coding. As I mentioned in my comment to arilaan, I had been reusing code from an exercise that compared strings and completely forgot about the change in data types, so a big thanks to you both for pointing that out! The function is working properly now. – Nea Nov 19 '13 at 05:54