Save Function Output in a loop

4 views (last 30 days)
David Pfurtscheller
David Pfurtscheller on 27 May 2019
Answered: 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

Answers (1)

Geoff Hayes
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.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!