First attempts at MATLAB GUI with embedded UDP objects
Sunday, May 18th, 2008This is what I did to connect a MATLAB GUI to a UDP object (using the Instrument Control Toolbox). This example is a bit over-simple (one instance of MATLAB - nothing remote), but the concept could certainly be modified for a real application (in fact, I need to for a work project…). Please bear with me - I’ve never used event based programming in MATLAB before. If I’ve done something wrong (and I’m sure I have), please let me know!
First the basics: A GUIDE GUI:
Two pushbuttons, an edit box, a static text we don’t care about (title text), and one we do… configuring these further:
I set tags for the important items. The pushbuttons are named ’send’ and ‘close’ respectively, the text box is called ’sendbox’ and the static text is called ‘rcv’. More importantly:
The HandleVisibility for the GUI itself must be set to on (and less importantly, the name can be set here as well).
With this prep, the real work is ready to begin. In the gui_n_udp_OpeningFcn, set-up the UDP objects:
% Choose default command line output for gui_n_udp
handles.output = hObject; % (GUIDE Configured)
% Create UDP objects
handles.rcv_udp = udp;
handles.send_udp = udp;
% Set-up UDP Receiver
handles.rcv_udp.remotehost = 'localhost'; % Normally an IP Address
handles.rcv_udp.remoteport = 10001;
handles.rcv_udp.LocalPortMode = 'manual'; % Required
handles.rcv_udp.LocalPort = 3457;
handles.rcv_udp.LocalHost = 'localhost'; % Unnecessary?
handles.rcv_udp.Tag = 'rcv_udp'; % This can make things easier...
% Set-up UDP Sender (you should be able to see how this could be applied to
% two separate MATLAB computers)
handles.send_udp.remotehost = 'localhost'; % Normally an IP Address
handles.send_udp.remoteport = 3457;
handles.send_udp.LocalPortMode = 'manual'; % Required
handles.send_udp.LocalPort = 10001;
handles.send_udp.LocalHost = 'localhost'; % Unnecessary?
handles.send_udp.Tag = 'send_udp';
% Set-up callback for a UDP event (Receiver only)
% There must be a better way to do this?
handles.rcv_udp.DatagramReceivedFcn = 'UDP_GUI(''display_udp'')';
% Prep the UDP objects!
fopen(handles.rcv_udp);
fopen(handles.send_udp);
% Update handles structure
guidata(hObject, handles); % (GUIDE Configured, but necessary of course)
As the comments describe, I’m setting up two UDP objects that are linked together. It’s pretty boring (everything happens on the local machine), but the concepts are the same if IPs are used. I’m putting these directly into the GUI handles. The hardest part to piece through is that a GUI is a figure which can contain nearly unlimited additional variables and structs within it. Some of these objects are graphical while others, like the UDP objects I create, are just hanging out as children of the GUI figure… more about that later on.
You’ll also notice that I edit the handles.rcv_udp.DatagramReceivedFcn callback function. When a UDP Datagram is received, the event is triggered and the command in that variable is executed. I’m not really sure how to use function calls (e.g. “@display_udp”) so I used an explicit call to the GUI function. There may be a better way (as I indicated…). I won’t be jumping to this function yet…
Next up is the send_Callback. This is the function that is called when the Send button is pressed and its contents are pretty simple:
fwrite(handles.send_udp,get(handles.sendbox,‘String’));
All this does is send a string (retrieved from the ’sendbox’ text box) to the send_udp object. Hooray, we’re internet ready…
Now on to the display_udp function:
function display_udp()
% Takes a UDP event from rcv_udp and sets it to the rcv
gui_handle = findobj('Tag','gui_n_udp');handles = guidata(gui_handle);
data = fscanf(handles.rcv_udp,'%c');set(handles.rcv,'String',data);guidata(gui_handle,handles);
This function was tougher - and probably somewhere things could have been done more easily. I first need to find the GUI handle. This handle is the ID (an actual floating point number) that lets me access the contents of the GUI. This is the point where, if HandleVisibility is turned off (or “Callback”), you won’t be able to search out the GUI’s Handle ID.
The “guidata” function retrieves the GUI Handles (plural - this is the GUI object that holds all the other goodies). Once this object has been retrieved, it’s easy to scan the data from the rcv_udp object, write the data to the “rcv” static text, and save everything using the guidata function again (an important step).
Finally, we have the close_Callback function (activated when the CLOSE button is pressed):
fclose(handles.rcv_udp); fclose(handles.send_udp); delete(handles.gun_n_udp);
There’s not much exciting here… if you don’t close out the UDP objects, the ports will be seized until MATLAB is restarted. Deleting the figure object closes the GUI.








