Building a Table Within a While Loop
Show older comments
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?
Answers (1)
Image Analyst
on 29 Apr 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 Comments
Matt Hammerstein
on 29 Apr 2016
Image Analyst
on 29 Apr 2016
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!