Why does my for loop cause MATLAB to not stop running?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Matt J
on 26 Jan 2022
Edited: Matt J
on 26 Jan 2022
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 Comments
Matt J
on 26 Jan 2022
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
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!