How to create a for loop function, which calls on other functions?

So I have a for loop that looks like this;
for i = 1:20
[PTOTAL] = Subroutine_3_Power(AUM, FS, D100, VTM, BLOCKM, RHO, AM, ALIM, BLOCKT, LBOOM, RM, VTT, ALIT, AMUCD, KIM, NM, CM, CD0M, AT, NT, CT, RT, KIT, CD0T, PAUX, TRLF, DRAT);
WF=Fuelflow(NENG, AEF, PRAT, TRAT, BEF, PTOTAL, TLEG);
FUSED = WF*TLEG;
fprintf('Fuel used = %f kg\n', FUSED);
fprintf('FUSED-FUSED0 = %f\n', FUSED-FUSED0)
if(abs(FUSED-FUSED0)<=2)
AUM = AUM0 - FUSED;
fprintf(2,'All up mass = %f kg\n', AUM);
break
else
AUM = AUM -0.5*FUSED;
[PTOTAL] = Subroutine_3_Power(AUM, FS, D100, VTM, BLOCKM, RHO, AM, ALIM, BLOCKT, LBOOM, RM, VTT, ALIT, AMUCD, KIM, NM, CM, CD0M, AT, NT, CT, RT, KIT, CD0T, PAUX, TRLF, DRAT);
WF=Fuelflow(NENG, AEF, PRAT, TRAT, BEF, PTOTAL, TLEG);
end
end
as you can see, the loop contains 2 other functions; [PTOTAL] and WF. I want to make the for loop a function, so that I don't have to retype it every time I use it.
However, I dont even know where i would start with this, as the two functions within the loop contain so many variables.. does this mean that when I create the new for loop function, I have to relist all the variables in the PTOTAL and WF function, such that it would look like this;
function [loop] = myloop(PTOTAL, WF, AUM, FS, D100, VTM, etc, etc, etc....)
Thank you.

1 Comment

Essentially yes, though you can pack all those arguments into a vector, or possibly better a struct. Then you can just pass the struct - it still needs to be created though.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 16 Apr 2021

Commented:

on 16 Apr 2021

Community Treasure Hunt

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

Start Hunting!