How to prevent function output being overwritten in for loop?

1 vue (au cours des 30 derniers jours)
John
John le 19 Nov 2013
Commenté : John le 19 Nov 2013
Hi there,
I have a for loop below which calls the same function "Optimiseenergy" in each iteration. There are 2 outputs from this function "Energyin" and"Energyout". Could anybody suggest how I can prevent the outputs from being overwritten in each iteration? Ideally I would like to save the outputs as Energyin1, Energyout1, Energyin2, Energyout2... etc
I tried changing the function call to this but it doesn't work.
[Energyin(i),Energyout(i)] = Optimiseenergy(at(i),dt(i+1),SOC(i),20);
Thank you
JN = 3; %
dt=[11 29 38]; % departure times
at=[14 33 42]; % arrival home times
SOC(1)=SOC0;
for i=1:1
SOC(i) = SOC0 - (at(i)-dt(i))*1.22;
[Energyin,Energyout] = Optimiseenergy(at(i),dt(i+1),SOC(i),20);
end
for i=2:JN-1
SOC(i) = 20 - (at(i)-dt(i))*1.22;
[Energyin,Energyout] = Optimiseenergy(at(i),dt(i+1),SOC(i),20);
end

Réponse acceptée

Simon
Simon le 19 Nov 2013
Modifié(e) : Simon le 19 Nov 2013
Hi!
Use arrays to save the results:
% allocate arrays for results
Energyin = zeros(JN-1, 1);
Energyout= zeros(JN-1, 1);
for i=2:JN-1
SOC(i) = 20 - (at(i)-dt(i))*1.22;
% save results
[a, b] = Optimiseenergy(at(i),dt(i+1),SOC(i),20);
Energyin(i) = a;
Energyout(i) = b;
end
  3 commentaires
Simon
Simon le 19 Nov 2013
Modifié(e) : Simon le 19 Nov 2013
Then, store them in a cell array.
% allocate arrays for results as matrix
Energyin = cell(JN-1, 1);
Energyout= cell(JN-1, 1);
for i=2:JN-1
SOC(i) = 20 - (at(i)-dt(i))*1.22;
% save results
[a, b] = Optimiseenergy(at(i),dt(i+1),SOC(i),20);
Energyin{i} = a;
Energyout{i} = b;
end
John
John le 19 Nov 2013
Good idea, thanks very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by