I know the Matlab GUI callbacks have been discussed here hundreds of times, but I think I ran into a special problem.
I know how to share data between the different GUI callbacks (I use the handles-structure as described in the documentation). I created my GUI with GUIDE.
There is one pushbutton that creates a bluetooth-handle in its callback function, which works just fine. Once the Bluetooth connection is established and a certain amount of bytes is received, a bluetooth-callback function is triggered (set by bluetoothhandle.BytesAvailavailableFcn = @rdatac_Callback
).
The header of my Bluetooth callback-function looks like this:
function rdatac_Callback(hObject, eventdata)
The hObject
contains the bluetooth-handle, eventdata
the event which triggered the callback (in this case it's called "BytesAvailable").
This function is in the same file as all other GUI callbacks. The problem is that I cannot access the handles structure of the GUI within the bluetooth-callback. I tried adding "handles" as a third input parameter, but then it's empty. I also tried generating my own handles structure by
h = guidata(findobj('Name','BCI'));
where BCI is the name of the GUI window. This works perfectly in any GUI-callback function, but inside the Bluetooth callback it cannot find the other elements, so the Bluetooth callback has its very own "scope".
So my question is: How can I share all the GUI handles with my Bluetooth callback function? My workaround was using the global workspace, but I would like to use the handles structure if possible.
Create your bluetooth callback with the signature function rdatac_Callback(hGuiObject, hBtObject, eventdata)
. Now when creating the bluetooth object in your GUI-Callback you have the handle to your GUI-Object. This should be passed to your rdatac_Callback. You can achive this using the function handle @(hBtObject,eventdata)rdatac_Callback(hGuiObject, hBtObject, eventdata)
. The function rdatac_Callback
is always called with the three parameters you need, but only two have to be passed to the anonymous function. hGuiObject
is taken from the workspace where you created the anonymous function.