How to extract rows from a table using a loop to search for specific column values, and create a new table with extracted rows

8 views (last 30 days)
I have an excel document containing information that details flight arrival and departure information for a full year. What I would like to do is find a row based on the information stored in two seperate columns, extract this row and create a new table containing this row.
The columns are column 1, and column 7; column 1 is the arrival/departure indicator, and column 7 is the airport name. Both are text based. I wish to first extract the Arrival row, and then the corresponding Departure row. The column 7 text will be the same for both rows. The loop would allow for the journey to be identified and extracted every time it takes place for the entire year.
I have imported the excel file using the 'readtable' function. However, I have tried and failed a few methods I have found on the forum to complete the next step but have had no luck. Any help is appreciated.

Accepted Answer

dpb on 22 Jun 2021
Edited: dpb on 22 Jun 2021
Would be much easier if attached a subset of the input file or a .mat file containing the table...we have to make up stuff to try to fit the verbal description. From the sounds of it, I'd guess the "column 7" variable is the flight number.
Here's the power of MATLAB -- you "don't need no steenkni' loops!" to do this; just logical addressing
isWanted=contains(tFlights.Flight,'WantedFlight'); % get the flight number
tFlightWant=tFlights(isWanted,:); % return all records for the flight number
will give you the complete selection for the given flight; it's immaterial for the selection process about whether is Arr or Dep on the direction; if the original table is in chronological order, then they should already be paired in terms of Dep/Arr times.
The above, of course, makes a whole lot of presumptions about details that aren't provided that would be required to write real code.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!