How to pass variables to a callback function and back again?

조회 수: 3 (최근 30일)
John Doe
John Doe 2011년 8월 16일
Hello, I have a callback function that is executed every so often. What I wish to do is pass a matrix and an integer (e.g. x = []; y = 1;) to the callback, have them manipulated and then outputted to the parent program.
x = [];
y = 1;
t = timer('TimerFcn',@imagecap,'ExecutionMode','fixedRate','Period',0.3333, 'UserData', x, y);
start(t)
if epoch == 31
stop(t)
end
%--------------
function [x, y] = imagecap(obj, event, arg1, arg2)
I've looked at the matlab help but that section seems rather obtuse.

채택된 답변

Paulo Silva
Paulo Silva 2011년 8월 16일
The timer code, TimerFcn is executed in the base workspace so the reason for the assignin lines (just in case you use the code inside a function and not in scripts)
%These are the initial values of your variables
assignin('base','x',100)
assignin('base','y',200)
t = timer('TimerFcn','[x, y] = imagecap(x, y)',...
'ExecutionMode','fixedRate','Period',0.3333);
start(t)
Example TimerFcn, just increments the x and y values
function [x,y]=imagecap(x,y)
x=x+1;
y=y+1;
disp(x)
disp(y)
end
Always remember to do delete(timerfindall) so you don't have timer objects using memory after you use them.
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 8월 16일
Here you go! That's the answer. Just put in the return arguments in the callback of the timer object.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Code Execution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by