Is it possible to save an output of a function and pass it as an input to another function after some time steps?

1 次查看(过去 30 天)
So, I have three functions that I call every time step: for ii = 2:50 [G_dmg,Output2,output3] = Stages(G_dmg, Input2, Input3);
[G_dmg, output2] = decision(G_dmg);
% G_dmg is an object of three fields, Only the field "G_dmg.Nodes.Load"
% is changing and I want to store this field ( or the whole object) in each iteration.
if ~mod(ii,4) % if this condition is satisfied ( that is when ii=4,8,12,...) I want to pass the field "G_dmg.Nodes.Load"
% saved at (ii = 2,3,4,... and not the ones saved at ii = 4,8,12,...) to function implement
[G_dmg] = implement(G_dmg, Input2, Input3)
end
end
To simplify my comments:
The condition will be satisfied first at ii = 4, at this iteration I want to pass G_dmg that was saved at ii = 2. And at ii = 8 I want to pass the one that was saved at ii = 3 and so on.
I would appreciate your ideas.

采纳的回答

Kishan Dhakan
Kishan Dhakan 2021-6-30
编辑:Kishan Dhakan 2021-6-30
Try:
iter = 1
G_dmg_at_index_ii = []
for ii = 2:50
[G_dmg,Output2,output3] = Stages(G_dmg, Input2, Input3);
[G_dmg, output2] = decision(G_dmg);
G_dmg_at_index_ii(end+1) = G_dmg;
% G_dmg is an object of three fields, Only the field "G_dmg.Nodes.Load"
% is changing and I want to store this field ( or the whole object) in each iteration.
if ~mod(ii,4) % if this condition is satisfied ( that is when ii=4,8,12,...) I want to pass the field "G_dmg.Nodes.Load"
% saved at (ii = 2,3,4,... and not the ones saved at ii = 4,8,12,...) to function implement
[G_dmg] = implement(G_dmg_at_index[iter], Input2, Input3)
iter = iter+1;
end
end
Note: There will be a warning saying 'preallocate G_dmg_at_index_ii for performance', which can either be ignored or resolved by calculating the required size and initialising with dummy G_dmg objects.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Networks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by