How to use GUI handles in external function?

4 views (last 30 days)
Tautvydas Belgeras
Tautvydas Belgeras on 4 Dec 2017
Edited: Greg on 9 Dec 2017
I have a task, where a server reads incoming messages and works with them to eventually display them on an axis in a GUI. For this task, BytesAvailableFcn is perfect, but I have some problems implementing it. I am able to pass the handles structure to it and to use the variables inside, but I am not able to update it in the end. Parts of my code, that are relevant:
%Configuration of the port
handles.TCPL = tcpip('0.0.0.0', 31010, 'NetworkRole', 'server');
handles.TCPL.BytesAvailableFcnCount = 23;
handles.TCPL.BytesAvailableFcnMode = 'byte';
handles.TCPL.BytesAvailableFcn = {@nuskaitymas,handles};
%
%
%
%
%The callback function-----------------------------------------------------------
function nuskaitymas(hObject,eventdata,handles)
%Takes the handles structure, uses variables inside it
.
.
.
%This part of the code is not relevant
.
.
.
%Update handles structure
guidata(hObject, handles);
end
----------------------------------------------------------------------------------
What happens is when I call "guidata", the hObject gets marked with an error. Any ideas?
Also, what I have tried is instead of doing the calculations inside the callback, is to change a global variable inside the callback to stop a while loop inside the main function of the GUI, but what happens is that the program waits for the while loop to stop, before calling the BytesAvailableFcn.
  3 Comments
Tautvydas Belgeras
Tautvydas Belgeras on 7 Dec 2017
This is the error that I get
Error using guidata (line 87)
H must be the handle to a figure or figure descendent.
Error in nuskaitymas (line 80)
guidata(hObject, handles);
Error in instrcb (line 36)
feval(val{1}, obj, eventStruct, val{2:end});
Tautvydas Belgeras
Tautvydas Belgeras on 7 Dec 2017
Should I note that the callback function "nuskaitymas" is on a separate m file?

Sign in to comment.

Answers (1)

Greg
Greg on 9 Dec 2017
Edited: Greg on 9 Dec 2017
function nuskaitymas(hObject,eventdata,handles)
Is the callback for your tcpip object. The hObject argument is therefore the tcpip object itself (not a graphics component of your GUI) which is not compatible with guidata.
Change the last line to:
guidata(handles.figMyGUI,handles); % Change "figMyGUI" to any valid graphics object tag in handles

Community Treasure Hunt

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

Start Hunting!