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

1 次查看(过去 30 天)
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 个评论
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 CenterFile 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