Why is this code looping?

3 visualizaciones (últimos 30 días)
Amber
Amber el 10 de Jul. de 2012
%%Variables:
% aa,bb and x:counters
% bone: data of stress reading of a femer cantilevered with a different
% weight over a period of time
%
% averages1:averages of each basline
% averages2: x-coordinate where each jump occured
clc;clear;close all
time=-1:0.004:260.916;
load bone.mat
% plot(time,bone)
% grid
% hold on
bone2 = smooth(bone,750);
figure
plot(time,bone2,'g')
hold on
grid
while length(averages1)<=17 && aa>=length(bone2)
for x=1:1:70000; aa=x; bb=x+1;
difference=abs(bone2(aa)-bone2(bb));
if difference <= .009
flat(d)=[difference];
mean(difference)=averages1;
end
if difference >=.1
difference=abs(bone2(aa)-bone2(bb));
cc=length(bone2)-20000;
averages2(cc)=[find(bone2,max),find(bone2,min)]
end
end
I have written this code and it is supposed to find 17 averages and 17 jump points on the step function that gets plotted but it keeps looping after the 5th average. and I have no idea how to make the code find the jump points. Any help would be greatly appreciated.
  2 comentarios
Sebastian Holmqvist
Sebastian Holmqvist el 10 de Jul. de 2012
Well, it's hard to see what you actually want to achieve with this code. But I notice that you never update averages1 inside of your loop. And since you don't have any break or return command this means that the while loop will just keep going.
Amber
Amber el 10 de Jul. de 2012
it's suppose to compare the y-coordinates one by one and then when there's a big difference between them record the x-coordinate at that point. when there is a small difference between the (i.e. a straight line) it is supposed to give me the mean of that section of the plot

Iniciar sesión para comentar.

Respuestas (1)

John Petersen
John Petersen el 10 de Jul. de 2012
Editada: John Petersen el 10 de Jul. de 2012
You've got variables that aren't initialized, aa,d, and indices must be positive integers. 'difference' does not seem like a proper index. You clearly need to add an index, such as,
k = k+1;
(also initialize it) and change the line
mean(difference)=averages1;
to
averages1(k) = mean(difference);
You might have other problems with your code, for example, aa never changes which is another test to stay in the loop. Can't see how you ever get in the loop without initializing that one.
  3 comentarios
Amber
Amber el 10 de Jul. de 2012
Thank you though! :)
John Petersen
John Petersen el 11 de Jul. de 2012
that tells me that you're never satisfying the if(difference <=.009) test. Initialize averages1 and aa at least for good programming practice. Then single step through your program while observing how the variables are changing. This should give you some insight as to what's happening. Since I don't have your .mat files it's hard to debug it myself.

Iniciar sesión para comentar.

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