while a variable is true for a certain period of time

18 views (last 30 days)
hey, i am trying to make an if statement nested in a while loop by having a condition anded with a time period. if the condition is true for that period of time, do what ever is in the if statement. for example , let a variable called 'result'. when the user presses the button the while loop start calculations to get ' result'. what i want is, when the result value does not change for 25 second, another function will execute.
what i am really doing is calculating the delay between two microphones and with that calculating angle of the sound source, if the angle is 0, there are no sound. if there is no sound for 25 second,no one is speaking and the device is not being used, in that case i want to do some commands that will cut off power from devices attached to parallel port. i have done all needed codes but i dont know how to write an if statement that tells matlab do these lines of codes if the angle stays zero for 25 seconds.
any ideas ??

Answers (1)

Sara
Sara on 27 Mar 2014
You could call the function
c = clock; where c is [year month day hour minute seconds]
before the loop and call it at every iteration to see if 25 s have passed. So
initTime = clock;
newTime = initTime
while condition && newTime(6)-initTime(6) < 25
...code
newTime = clock;
end
  2 Comments
hassan
hassan on 27 Mar 2014
thanks, i think thats the way, but it kept giving me the error "Operands to the and && operators must be convertible to logical scalar values." what i did exactly is : initTime = clock; newTime = initTime time=newTime(6)-initTime(6); while ((ang==ang) && (time == T1)) %% (where T1 is the time got from GUI edit box and ang is the angle, so if the angle doesnt change for time of T1 do:) set(handles.text17,'String',num2str(123)) newTime = clock; end
and i put c=clock; before the loop
Sara
Sara on 27 Mar 2014
I'd try debugging looking if maybe ang or T1 are strings or arrays...That's usually why you get that error. Can you share the code you wrote? Details matter.

Sign in to comment.

Categories

Find more on Dates and Time 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!