Problems updating variable on the callback of a timer

10 views (last 30 days)
Ángel González
Ángel González on 19 Sep 2018
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');
while((x == false) && (timer_event == false))
% get samples
x = true;
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!

Answers (1)

Christopher Wallace
Christopher Wallace on 19 Sep 2018
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;
get(t, 'UserData')
>> get(t, 'UserData')
ans =
timer_event: 0
>> get(t, 'UserData')
ans =
timer_event: 1

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by