Save Function Output in a loop

3 visualizzazioni (ultimi 30 giorni)
David Pfurtscheller
David Pfurtscheller il 27 Mag 2019
Risposto: Geoff Hayes il 27 Mag 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

Risposte (1)

Geoff Hayes
Geoff Hayes il 27 Mag 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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by