Why does my for loop cause MATLAB to not stop running?

1 回表示 (過去 30 日間)
Goncalo Costa
Goncalo Costa 2022 年 1 月 26 日
コメント済み: Matt J 2022 年 1 月 26 日
The code I am writing has as its purpose to analyse some data and plot it accordingly. My data consists of a series of consecutive measurements but every now and then there is a malfunction in the physical system that causes a measurement to be wrong (this event is not regular but purely aleatory).
I am able to find which measurement is wrong before plotting them, and therefore I am trying to elimiate such measurements, but sometimes there is no wrong measurements.
I am trying to put 3 plots together, 2 with errors at 83 and 5, and one without errors (characterised as 0). I wrote the following for loop with if statements:
sample = [name1, name2, name3];
num = [83, 5, 0];
for x = 1: length(sample)
if num(x) ~= 0
for i = 1:(num(x)-1)
a(i)=sample(x).impulse_P2P(i);
end
for i=(num(x)+1):length(sample(x).impulse_P2P)
a(i-1)=sample(x).impulse_P2P(i);
end
sample(x).impulse_P2P=a;
elseif num(x) == 0
for i = 1:length(sample(x).impulse_P2P)
a(i)=sample(x).impulse_P2P(i);
end
sample(x).impulse_P2P=a;
end
But for some reason when I run this bit of code, it never stops running, I think it might be stuck in a loop or something like that but I don't know why.
Any help would be greatly appreciated.

採用された回答

Matt J
Matt J 2022 年 1 月 26 日
編集済み: Matt J 2022 年 1 月 26 日
In the code as you've posted it, the for-loop is never closed by an "end". That would prevent it from continuing to the next iteration.
  2 件のコメント
Goncalo Costa
Goncalo Costa 2022 年 1 月 26 日
Thank you so much, that was a super silly mistake.
Matt J
Matt J 2022 年 1 月 26 日
You're quite welcome, but please Accept-click the answer to indicate that it resolved the question.
Please also go back to your previous posts and do likewise

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

その他の回答 (0 件)

カテゴリ

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