Passing a listener from GUIDE function to a function outside of GUIDE?
Show older comments
Hello everyone - I hope this is a simple question, but I've been struggling for days to get this to work. I have made a GUI via GUIDE that acquires data in the background. However, I'm having trouble getting the function I call in addlistener to have the handle for the listener used. I've been able to get it working in a script by itself as an anonymous function, and I have been able to write a script that calls it and it works well. However, once I try to implement it into the GUI it does not work anymore. I think it may be an issue with updating handles and/or calling the handles from the GUI to the current m file function. Below is are my two functions, any help? Running R2014b.
% --- Executes on button press in Toggle.
function Toggle_Callback(hObject, eventdata, handles)
% hObject handle to Toggle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if get(handles.Toggle,'Value') == 1;
if exist('TempDataFile.txt', 'file') == 2
delete('TempDataFile.txt');
end
openDAQ = daqfind;
for n = 1 : length(openDAQ)
delete(openDAQ(n));
end
newDAQ=daq.createSession('ni');
newDAQ.addAnalogInputChannel('Dev1','ai0','Voltage');
newDAQ.addAnalogInputChannel('Dev1','ai1','Voltage');
newDAQ.IsContinuous = true;
newDAQ.Rate=10;
% newDAQ.DurationInSeconds=1;
lh = addlistener(newDAQ, 'DataAvailable', @plotData);
guidata(hObject, handles);
plotData(lh);
newDAQ.startBackground();
else
stop(newDAQ);
newDAQ.IsContiniuous = false;
end
And the function called in lh (which is plotData.m):
function plotData(newDAQ,DataAvailable, lh)
handles = guidata(gcbo);
%get(handles.lh, 'DataAvailable');
Time = [];
Data1 = [];
Data2 = [];
axes(handles.axes1);
plot(DataAvailable.TimeStamps, DataAvailable.Data(:,1), 'xb');
plot(DataAvailable.TimeStamps, DataAvailable.Data(:,2), 'og');
Time(:,1) = DataAvailable.TimeStamps;
Data1(:,1) = DataAvailable.Data(:,1);
Data2(:,1) = DataAvailable.Data(:,2);
TempData = [];
TempData(:,1) = Time;
TempData(:,2) = Data1;
TempData(:,3) = Data2;
dlmwrite('TempDataFile.txt',TempData, '-append');
end
Effectively, I'm trying to use the listener lh to obtain the data in a different function in GUIDE from where I initialized it and it still isn't working.
Thank you for your help in advance!
Answers (1)
Geoff Hayes
on 25 Aug 2016
Eric - save lh to the handles structure as
function Toggle_Callback(hObject, eventdata, handles)
% your code
% create listener
handles.lh = addlistener(newDAQ, 'DataAvailable', @plotData);
% save updated handles structure
guidata(hObject, handles);
Now, in every other callback that has handles as an input parameter, just access lh as
handles.lh
In your case though, you seem to be passing lh into plotData. Why is that? In fact, why does your function need to know about the listener? If you want to pass a third parameter in to the plotData function, then you would need to do
lh = addlistener(newDAQ, 'DataAvailable', {@plotData,handles.figure1});
where I'm assuming that figure1 is the name of your GUI. In the plotData function you would then do
function plotData(newDAQ,DataAvailable, guiHandle)
handles = guidata(guiHandle);
lh = [];
if isfield(handles,'lh')
lh = handles.lh;
end
% etc.
But...why does plotData need to know about lh?
2 Comments
Eric
on 26 Aug 2016
Geoff Hayes
on 26 Aug 2016
Hi Eric - guiHandle is the extra parameter that you pass in to the callback at
lh = addlistener(newDAQ, 'DataAvailable', {@plotData,handles.figure1});
The handles.figure1 (which may be named differently for your GUI) is the handle to the GUI. I still don't understand how you might go about using this listener in any of your other functions or callbacks though...
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!