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

1 view (last 30 days)
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.

Accepted Answer

Kishan Dhakan
Kishan Dhakan on 30 Jun 2021
Edited: Kishan Dhakan on 30 Jun 2021
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.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by