How to use GUI handles in external function?

1 次查看(过去 30 天)
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 个评论
Tautvydas Belgeras
Tautvydas Belgeras 2017-12-7
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 2017-12-7
Should I note that the callback function "nuskaitymas" is on a separate m file?

请先登录,再进行评论。

回答(1 个)

Greg
Greg 2017-12-9
编辑:Greg 2017-12-9
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!

Translated by