MATLAB Answers

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 日間)
Waseem AL Aqqad
Waseem AL Aqqad 2021 年 6 月 30 日
コメント済み: Waseem AL Aqqad 2021 年 7 月 2 日
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 件)

Community Treasure Hunt

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

Start Hunting!

Translated by