Send data from workspace to callback function

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)

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

Asked:

J
J
on 25 Aug 2015

Answered:

on 25 Aug 2015

Community Treasure Hunt

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

Start Hunting!