Problems updating variable on the callback of a timer

조회 수: 4 (최근 30일)
Ángel González
Ángel González 2018년 9월 19일
댓글: Christopher Wallace 2018년 9월 20일
Hi there! I'm trying to get a while-loop to exit depending on two variables; one of them is changed when a time is passed (using a timer). The other one is changed inside the while loop after a condition. All this while loop is also inside a for a loop.
Here is a draft of my code:
for i=i:N %%N defined previously
...
% Some operations
...
x = false;
timer_ event = false;
timer_1 = timer('StartDelay', time_limit, 'TimerFcn', 'timer_event = true');
start(timer_1);
while((x == false) && (timer_event == false))
pause(0.0334)
...
% get samples
...
if(%condition)
x = true;
end
end
delete(timer_1);
end
The question here is that the while-loop only breaks when the x variable changes the value, but nothing happens when the timer callback changes the value of the timer_event variable.
Thank you so much!

답변 (1개)

Christopher Wallace
Christopher Wallace 2018년 9월 19일
Use the 'UserData' property of the timer to access data set within the timer function.
Look at the function I've attached to create a timer that modifies the UserData every period.
After running the function if you were to start the timer and repeatedly query the UserData you'll see it changing from true to false.
t = createTimer;
start(t);
get(t, 'UserData')
returns
>> get(t, 'UserData')
ans =
timer_event: 0
>> get(t, 'UserData')
ans =
timer_event: 1
  댓글 수: 2
Ángel González
Ángel González 2018년 9월 20일
I will take a look at that, but I have been trying different things on my code. The StartDelay property of the timer depends on a value introduced in GUI. When I use a constant StartDelay, everything works fine...but when I try to use a StartDelay introduced via GUI, the timer goes wrong.
Christopher Wallace
Christopher Wallace 2018년 9월 20일
If you upload your code I can take a look.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by