MATLAB Answers

0

Save Function Output in a loop

Asked by David Pfurtscheller on 27 May 2019
Latest activity Answered by Geoff Hayes
on 27 May 2019
Hello,
I have a series of Inputs into a Function Input = (1,2,3). These Inputs are saves as a vector. Now I want to save the Output (several Values )for each Input with a while loop.
E.g The Output of the Functions consists out of 4 Values : ChargeLevel, Vbattery, Ibattery, Rbattery
But I always get an error. How to I properly save it into a variable so I have a table afterwards like this :
Input No. ChargeLevel Vbattery Ibattery Rbattery
1 100 25 10 5
.....
Input = ( 1, 2,3);
i=1;
while i<=3
z=AC.secondseg_exp( Input(i), timefirst, time);
i=i+1;
time=time+timefirst;
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 27 May 2019

David - what is the error? Which are the output parameters (in your above code) that you want to save on each iteration of the loop? Just the z? If so, then try
Input = [1, 2, 3]; % <---- note the square brackets
i=1;
zData = zeros(3,4);
while i<=3
z(i,:) = AC.secondseg_exp( Input(i), timefirst, time);
i = i + 1;
time = time + timefirst;
end
I'm assuming that the output from AC.secondseg_exp is a 1x4 array. If not, then you will need to adjust your code.
Note that you could replace the while loop with a for loop.

  0 Comments

Sign in to comment.