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

1 visualización (últimos 30 días)
Goncalo Costa
Goncalo Costa el 26 de En. de 2022
Comentada: Matt J el 26 de En. de 2022
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.

Respuesta aceptada

Matt J
Matt J el 26 de En. de 2022
Editada: Matt J el 26 de En. de 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 comentarios
Goncalo Costa
Goncalo Costa el 26 de En. de 2022
Thank you so much, that was a super silly mistake.
Matt J
Matt J el 26 de En. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by