How to execute a piece of code if a condition is verified for 'n' seconds

Hi, i need to do something like this:
if (my_condition is verified for 'n' seconds) do_this_piece_of_code; end
However i don't want that the execution stops for 'n' seconds when arrives there to evaluate the condition, but i want that anywhere the execution is, if that condition is verified for 'n' seconds, execute that piece of code. imho i need to write it out of my program.
Could you help me? Thanks in advance

2 Comments

You need to add more details, what is your condition?
There are several ways to do that: timer, simple double condition that checks time, while condition...
Thanks. So, the problem is this: i want that if mouse coordinates remain for 1 second around a circle of 5 pixel (for example) then it has to "click" (to "click" i have already seen the function to use)

Sign in to comment.

Answers (2)

Sounds like you will inevitably want timers at some point:
doc timer
You might get lucky and be able to use tic/toc though
doc tic
doc toc
Thanks for the answers. I thought also this: for me it is not fundamental that in "every" moment the coordinates have to be in the circle, it is sufficient that they are in some moments. For example, if i want that it has to not change for 1 second, i could use the coordinates of mouse as global variable, a function that checks their values every 0,2s and an array in the function that save the last 5 values; if the array has the same values (or better if the array has all the values in a circle of radius 'n'), then "click". Could be a good idea?

Categories

Asked:

on 17 Apr 2012

Community Treasure Hunt

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

Start Hunting!