Building a Table Within a While Loop

I am having trouble building a table within a while loop. Here is how I have the loop set up:
while mwater>0
(The program performs operations on many variables here)
printcounter=printcounter+1;
if printcounter==9
T=table(t,Pair,Temp,Fthrust,Fgravity,Fdrag,Fnet,mwater,mair,mtotal,arocket,vrocket,x);
printcounter==0;
end
end
The way I have it set up, the entire table is rewritten with each iteration of the loop. I need each iteration to add another row onto the table, does anyone know how I could do this?

Réponses (1)

Image Analyst
Image Analyst le 29 Avr 2016

0 votes

Build up each of your columns, in whatever way works, in advance. Use a loop if you have to. Then just call table() once after all columns have been constructed.

2 commentaires

Matt Hammerstein
Matt Hammerstein le 29 Avr 2016
Sorry, I have very little MATLAB experience. What would you suggest as the best way to build the columns?
As an example:
printcounter = 0;
mwater = 1;
loopCounter = 1;
while loopCounter<5
printcounter=printcounter+1;
if printcounter==9
printcounter=0;
end
t(loopCounter) = loopCounter * 2;
Pair(loopCounter) = loopCounter * 3;
Temp(loopCounter) = loopCounter * 4;
Fthrust(loopCounter) = loopCounter * 5;
Fgravity(loopCounter) = loopCounter * 6;
Fdrag(loopCounter) = loopCounter * 7;
Fnet(loopCounter) = loopCounter * 8;
mwater(loopCounter) = loopCounter * 9;
mair(loopCounter) = loopCounter * rand(1);
mtotal(loopCounter) = loopCounter * sind(90*rand(1));
arocket(loopCounter) = loopCounter * 10;
vrocket(loopCounter) = loopCounter * 11;
x(loopCounter) = rand
loopCounter = loopCounter + 1;
end
T=table(t',Pair',Temp',Fthrust',Fgravity',Fdrag',Fnet',mwater',mair',mtotal',arocket',vrocket',x')
The ' is to transpose the row vectors into columns. Adapt as you see fit.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by