How to pass a value to TIMER function

조회 수: 16 (최근 30일)
Feng
Feng 2013년 7월 21일
Hi, I would like to have the data I obtained from my NI daq board to show on my GUI window in real time while I can still run other things from the GUI. I tried the TIMER function, but not sure how to pass the value to the timer function. Any help and suggestion would be greatly appreciated.
Thanks,
Feng

답변 (1개)

dpb
dpb 2013년 7월 21일
Create a timer setting type and period initially...
rate=10; % a repetition rate
ht=timer('ExecutionMode','fixedrate', ...
'period', rate, ...
'TimerFcn',@yourcallbackfunction);
Set the rate later...
rate=rate/2;
set(ht,'period',rate);
doc timer % for more details
Not sure how you're trying to do this; you might want a singleshot and restart it from the callback when you've got a new set of data; not sure...
  댓글 수: 2
Feng
Feng 2013년 7월 21일
What I was trying to achieve is actually this: t=timer('ExecutionMode','fixedrate', 'period',rate, 'TimerFcn',@callbackfun); Mydata = data;
function data = callbackfun . .
data = 3; . . .
I tried @(data)callbackfun, also t.TimerFcn='data=callbackfun'. it seems that none of them would allow me to access the 'data'. Any suggestion or trick?
Thanks,
Feng
dpb
dpb 2013년 7월 21일
Yeah, you have to follow the rules established for callback functions--see the docs for the timer object for details.
The basic idea is that if you specify the callback commands directly as the value of the callback function property w/ the callback an anonymous function the commands are evaluated in the MATLAB workspace.
OTOH, if instead of specifying MATLAB commands directly as the value of a callback property, when you create a callback function, the first two arguments must be a handle to the timer object and an event structure. Other application-specific arguments must be in a cell array and this function operates in its own space.
It's too involved a subject to try to repeat the help doc's here; read them in their entirety going through the examples of how to create/use callback functions.

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

카테고리

Help CenterFile Exchange에서 Simultaneous and Synchronized Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by