Send data from workspace to callback function

2 views (last 30 days)
J
J on 25 Aug 2015
Answered: Walter Roberson on 25 Aug 2015
Hi,
How can I get a value from my workspace in my callback function?
Example
% Configure a new listener to process the incoming data.
lh = addlistener(s,'DataAvailable', @stopWhenExceedOneV);
function stopWhenExceedOneV(src, event, ValueFromWorkspace)
if any(event.Data > 1.0*ValueFromWorkspace)
disp('Event listener: Detected voltage exceeds 1, stopping acquisition')
% Continuous acquisitions need to be stopped explicitly.
src.stop()
plot(event.TimeStamps, event.Data)
else
disp('Event listener: Continuing to acquire')
end
end
In this example the ValueFromWorkspace is a variable I would like to pull in from my workspace. Regards, J

Answers (1)

Walter Roberson
Walter Roberson on 25 Aug 2015
If ValueFromWorkspace changes over time and you need the newest version:
Define stopWhenExceedOneV as a nested function in the workspace that defines ValueFromWorkspace, and the definition must be after ValueFromWorkspace is initialized in the outer function. You would not have ValueFromWorkspace as one of the parameters of stopWhenExceedOneV
However, if ValueFromWorkspace is constant then leave your function as it is add use
lh = addlistener(s,'DataAvailable', @(src,event) stopWhenExceedOneV(src, event, ValueFromWorkspace));

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!