Filter löschen
Filter löschen

How to run a function each certain amount of time during loop?

14 Ansichten (letzte 30 Tage)
iddqd
iddqd am 21 Aug. 2017
Kommentiert: Jan am 23 Aug. 2017
I am running some kind of neurology experiment where i need to record cursor location and movements. I am doing this using infinte loop with Getmouse() inside (and also other manipulations on the cursor position) and write the position in log test file.
so far so good, the thing is that i need every few seconds to run a sync function which i already wrote (which send signal to serial port and write this event to the same log file from before).
How can i define event or interrupt in order to stop the loop every few seconds?
I am hoping i made my self clear and thanks.

Antworten (1)

José-Luis
José-Luis am 21 Aug. 2017
Looks like you need a timer object inside your function.
  2 Kommentare
iddqd
iddqd am 23 Aug. 2017
Hi thanks
tried your idea but having very weird thing.
i can see that the timer has finished but i don't know why it does not get into the if and break command.
any idea? (my goal is to break from the function after 4 seconds of the cursor inside a circle)
firstInside = 0;
timeout=0;
while (1)
[x, y] = GetMouse(cfg.window);
Cursor = CenterRectOnPointd(cfg.cursorShape, x, y);
if (mod(new_counter,20) == 0)
Screen('FrameOval', cfg.window , [cfg.TARGET_COLOR' cfg.CURSOR_COLOR'],[Target' Cursor'],4);
Screen('flip', cfg.window);
end
inside = IsInRect(x, y, Target);
new_counter = new_counter+1;
if inside
if ~firstInside
insideTimer = timer('TimerFcn', 'disp(''timer finished'');timeout=1','StartDelay',3);
firstInside=1;
start(insideTimer);
disp('timer started');
timeout = 0;
end
if timeout==1
return
end
Jan
Jan am 23 Aug. 2017
Do not create the timer callback as a string, because such strings are evaluated in the base workspace, such that the change of timeout is not visible inside the function.

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by