Save Function Output in a loop

4 visualizaciones (últimos 30 días)
David Pfurtscheller
David Pfurtscheller el 27 de Mayo de 2019
Respondida: Geoff Hayes el 27 de Mayo de 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

Respuestas (1)

Geoff Hayes
Geoff Hayes el 27 de Mayo de 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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by