Save results data to a new table each loop iteration

40 views (last 30 days)
The following loop generates a table (T_Departures) and saves 'Energy Calculation' data to this table. This overwrites every iteration however. I want to save the data to a new table each iteration (there are 30 iterations). When all iterations are complete, I wish to merge all results tables into one large table.
I have managed to save each result to file by using 'save' at the bottom of the loop, but I don't know how to then create a results table with the saved data. The load function either overwrites T_Departures with the saved data, or creating a new variable then loads the table into a struct. Any alternative methods or solutions to my current problem?
% set desired reg code search parameter
for i = 1:s
Aircraft = T_Reg(i,:);
Aircraft = table2cell(Aircraft);
Aircraft = Aircraft{1};
Aircraft = char(Aircraft);
% get flight data from table
Flights = contains(T_Data.Registration,Aircraft);
% return all rows for the flight data
T_Routes = T_Data(Flights,:);
% remove first row if movement type is 'D', else keep
if T_Routes.MovementType(1) == 'D'
T_Routes(1,:) = [];
% remove final row if movement type is 'A', else keep
if T_Routes.MovementType(end) == 'A'
T_Routes(end,:) = [];
% -------------------- Route On-Ground Time Calculation ------------------
% ------------------------------------------------------------------------
% Determine on ground time between each arrival and departure
OnGround = T_Routes.Date_Time(T_Routes.MovementType=='D') - T_Routes.Date_Time(T_Routes.MovementType=='A');
% New table with departure rows only
T_Departures = T_Routes(T_Routes.MovementType=='D',:);
OnGround = array2table(OnGround,'VariableNames',{'OnGroundTime'});
T_Departures = [T_Departures OnGround];
% ------------------------- Energy Calculation ---------------------------
% ------------------------------------------------------------------------
% % import fleet excel spreadsheet
T_Distance = readtable('Airport distances.xlsx',"TextType","string");
T_Distance.AirportIATA = erase(T_Distance.AirportIATA,"'");
T_Fuel = readtable('Aircraft Fuel Consumption.xlsx',"TextType","string");
% link distances and fuel spreadsheets to departures
T_Departures = join(T_Departures,T_Distance,"LeftKey","AirportIATA","RightKey","AirportIATA");
T_Departures = join(T_Departures,T_Fuel,"Keys","AircraftICAO");
T_Departures.kg_Fuel = T_Departures.Distance_km_.* T_Departures.kg_kmOfFuel;
% kWh per kg of kerosene
Kerosene = 12.67;
% efficiency of turboprop aircraft
PropEff = 0.5*0.98*0.8;
% efficiency of electric aircraft
AircraftEff = 0.98*0.95*0.98*0.8;
% energy requirement of aircraft
T_Departures.AircraftKWH = (Kerosene*PropEff*T_Departures.kg_Fuel)/AircraftEff;
save([Aircraft], 'T_Departures')

Accepted Answer

Walter Roberson
Walter Roberson on 3 Aug 2021
All_T_Departures = cell(s, 1);
for i = 1 : s
All_T_Departures{i} = T_Departures;
If you want, at this point
save(Aircraft, 'All_T_Departures')
If all of the tables have the same variables, then
All_T = vertcat(All_T_Departures{:});

More Answers (0)


Find more on Guidance, Navigation, and Control (GNC) 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!