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

조회 수: 2 (최근 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에서 Directed Graphs에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by