I am trying to extract all the values from a for loop

4 ビュー (過去 30 日間)
Sam17
Sam17 2019 年 4 月 26 日
編集済み: James Tursa 2019 年 4 月 26 日
I am trying to extract all the values from my for loop. the total data is 3 that satiesfies my condition inside the loop but, when i see on the workspace, it has only one variable after the loop.
Here is the code:
time=[];
for ii=2:size(time)-1
if dt(ii)>=30 && Vol(ii)>23.5 && Volt(ii)>=23
disp(times(ii));
time=n_time(ii);
end
end
time(1,:)=time;
When i see the displayed results it has 3 times, but, when i check the value inside the time vector it has only one value. Can you help me to extract all the 3 values which i getting, the one which satisfies the condition.

回答 (1 件)

James Tursa
James Tursa 2019 年 4 月 26 日
編集済み: James Tursa 2019 年 4 月 26 日
You could use a counter:
k = 0: % initialize outside loop
:
k = k + 1;
time(k) = n_time(ii);
That being said, if you initilize time=[] as an empty variable, how do you expect your loop to do anything if the upper index bound uses size(time)? Maybe you meant numel(times) here instead. Or maybe a vectorized approach without a loop would be better:
time = times(dt>=30 & Vol>23.5 & Volt>=23);
  1 件のコメント
Sam17
Sam17 2019 年 4 月 26 日
Actually after it satisfies the condition it should get the values from n_time, n_time is an array of timestamps
time=n_time(ii);
So all the values that satisfies the condition are 3 values. That's why i need those values stored in an array called time.

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

カテゴリ

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