MATLAB: How to save the while loop results for all iteration in 1 csv file

store result value in csv

hi, i need to store the best cost value for all iteration.
my code is,
while(i<maxitr)
i=i+1;
// function calling
if best_cost < best_cost1 && best_cost < best_cost2
best_cost= best_cost
elseif best_cost1 < best_cost && best_cost1 < best_cost2
best_cost= best_cost1
else
best_cost= best_cost2
end
THANKS IN ADVANCE

Best Answer

After the if/elseif/else, put in
best_costs(i) = best_cost;