Borrar filtros
Borrar filtros

variable's scope, persistent variables and timer !!!!

4 visualizaciones (últimos 30 días)
Bolivar
Bolivar el 3 de Sept. de 2013
Hello!
I want to access a persistent variable from within timer StopFcn callback function. The callback should delete the timer and empty the persitent variable. my code look like this:
function callerfunc()
persitent resp;
persistent hTimer;
if isempty(resp)
resp=0;
end
if isempty(hTimer)
timeout=5;
hTimer =timer('TimerFcn',@(h)fprintf(' %s callerfunc''s Timer is ran out ...'),'StartDelay',timeout);
hTimer.StopFcn = {@calledfunc, hTimer,resp};
end
start(hTimer);
end
function calledfunc(htimer,hresp)
delete(htimer);
hresp=[];
end
both functions are in the same file. Actually the call of calledfunc is a call by value and not by reference. therefore the persistent variable resp in callerfunc remaain unchanged after StopFcn excecute.can someone help me??
thanks!
Bolivar

Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Sept. de 2013
You cannot do that. Nest your second function instead of having it just sequential.
function callerfunc()
persitent resp;
persistent hTimer;
if isempty(resp)
resp=0;
end
if isempty(hTimer)
timeout=5;
hTimer =timer('TimerFcn',@(h)fprintf(' %s callerfunc''s Timer is ran out ...'),'StartDelay',timeout);
hTimer.StopFcn = @calledfunc;
end
start(hTimer);
function calledfunc(varargin)
delete(htimer);
resp=[];
end
end

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by