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?

回答(1 个)

Image Analyst
Image Analyst 2016-4-29
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 个评论

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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by