Save While-loop to vector

9 ビュー (過去 30 日間)
fxo
fxo 2013 年 4 月 24 日
編集済み: ranbo95 2017 年 3 月 21 日
I have this while loop which works very well and gives the correct output data but as it ends it only saves the latest datapoint, how can I save every loopdata into a vector?
t0=0.15; % Initial time
v0=46.5285; % Initial velocity
h0=3.4896; %Initial height
dt=0.001; % Timesteps/Precision
m=0.05; %Mass
g=9.81; % The gravitational constant
Velocity2=46.5285;
t = t0;
while Velocity2>=-20
Velocity2=hastighet(acceleration(0,m,g),t,v0,t0);
Height2=hojd(acceleration(0,m,g),t,h0,v0,t0);
t=t+dt;
end
Really appreciate your help!
  1 件のコメント
ranbo95
ranbo95 2017 年 3 月 21 日
編集済み: ranbo95 2017 年 3 月 21 日
Try assigning a zeros matrix first then assigning values to it, i.e;
i=1
%put your other details here
while XXX
vhmatrix=(100,2) %assigning a matrix with 2 columns and 100 rows
vhmatrix(i,1)=Velocity2(i)
vhmatrix(i,2)=Height2(i)
i=i+1
end
i is your index, which you need to refer to a cell of the matrix. Try using something like this!

サインインしてコメントする。

採用された回答

Mahdi
Mahdi 2013 年 4 月 24 日
Change it to the following:
VelocityVector=[];
while Velocity2>=-20
Velocity2=hastighet(acceleration(0,m,g),t,v0,t0);
VelocityVector=[VelocityVector; Velocity2];
Height2=hojd(acceleration(0,m,g),t,h0,v0,t0);
t=t+dt;
end
Then all your data is saved in VelocityVector.
  2 件のコメント
fxo
fxo 2013 年 4 月 24 日
Thank you SO much, really great!
not
not 2017 年 3 月 12 日
excellent help

サインインしてコメントする。

その他の回答 (2 件)

Matt Kindig
Matt Kindig 2013 年 4 月 24 日
編集済み: Matt Kindig 2013 年 4 月 24 日
Make your variables into vectors, by indexing through them:
index = 1;
while Velocity2(index) >=-20
Velocity2(index)=hastighet(acceleration(0,m,g),t,v0,t0);
Height2(index)=hojd(acceleration(0,m,g),t,h0,v0,t0);
t(index)=t(end)+dt;
index = index+1;
end
  1 件のコメント
toosky
toosky 2016 年 12 月 11 日
Great answer, very helpful for my current problem. Thanks!

サインインしてコメントする。


rahul bathini
rahul bathini 2015 年 6 月 11 日
can some one give me a simpler example like storing the value of 2 table a1=2,a2=4,a3=6.........

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by