ButtonDownFcn of a tool on Matlab GUIDE toolbar is not working - matlab

I have written a GUI program in Matlab using Matlab GUIDE.
Created a tool on the toolbar and wrote a right click call back function as shown below, but when I do a right click on that tool on the GUI, there is no response.
function MyTool_ButtonDownFcn(hObject, eventdata, handles)
waitfor(msgbox('Hello button down function','Hello button down'));
disp('Hello disp');
From property inspector, value of ButtonDownFcn is:
#(hObject,eventdata)main('MyTool_ButtonDownFcn',hObject,eventdata,guidata(hObject))
I tried to debug using another button with Callback function to execute 'keyboard'.
function bugfindbutton_ClickedCallback(hObject, eventdata, handles)
keyboard
This confirmed the existance of function MyTool_ButtonDownFcn(hObject, eventdata, handles) associated with ButtonDownFcn of the tool and is not deleted by any other functions. But a right click on the tool gives neither msgbox nor command window output.
I want right click of mouse on 'MyTool' toggle button (a custom tool on the toolbar) to execute MyTool_ButtonDownFcn function. How can I do it?

The ButtonDownFcn is ignored for uitoggletool and uipushtool objects. You should instead use the ClickedCallback property to set a callback. That callback, however, will only execute on a left mouse click and will not be triggered on a right mouse click.
If you really need to bind a callback to a right mouse click, you can use the findjobj utility from the file exchange to access the underlying Java object and assign a custom callback
h = uitoggletool( ...
'CData', rand(16, 16, 3), ...
'OnCallback', #(s,e)disp('on'), ...
'OffCallback', #(s,e)disp('off'));
% Get the underlying java object using findobj
jtoggle = findjobj(h);
% Specify a callback to be triggered on any mouse release event
set(jtoggle, 'MouseReleasedCallback', #(s,e)callback(h,e))
function callback(src, evnt)
if evnt.getButton() == 3
disp('Right Click!')
end
% Get the handles struct
handles = guidata(src);
% Modify the handles
handles.value = 2;
% Save the handles
guidata(src, handles);
end

Related

When mimicing a user click to invoke a callback, the root property CallbackObject is not updated?

I have a MATLAB m-file (mainGUI.m) which produces a GUI: a figure that contains some uicontrol objects (pushbuttons, menus, etc), each with a specific callback function. When I click around in the GUI, everything works fine, it does exactly what it is supposed to.
Now, I have another m-file (external.m) which, when executed, calls the m-file above, and mimics one of the callbacks (myCallbackfcn.m), as if the user was actually pushing a button. Below is the relevant pieces of code that executes inside external.m:
% Launch the GUI
mainGUI();
% Obtain the handles-structure, and the handle to the pushbutton
handles = guidata(findall(0,'type','figure'));
hObject = handles.myPushButton;
% Execute the callback
myCallbackfcn(hObject,[]);
This also works fine - inside the myCallbackfcn, the hObject is indeed the correct handle, I can get access to the entire GUI handle structure, and so on... But there is one flaw, which is crucial to the purpose of my code. The root property CallbackObject is empty! Whereas, it should be the handle to the hObject, which it is when I initiate the callback manually by clicking on the pushbutton.
Does anyone have an idea of why CallbackObject is not updated? It is read-only, so I can't change it by force.
The CallbackObject of the root object is empty because you are not executing the callback from an actual GUI event. The CallbackObject is automatically populated by MATLAB when you interact with a GUI widget.
Within your callback, rather than relying on the CallbackObject (or gcbo), you can get the object directly from your handles struct.
myCallbackfcn(hObject, [], handles)
function myCallbackfcn(hObject, eventdata, handles)
callbackObject = handles.myPushButton;
% Or more simply
callbackObject = hObject;
% Do stuff
end

Loading a stream of images in Matlab GUI

Here is my code for the callback function.
function Next_Callback(hObject, eventdata, handles)
display('Click Next');
handles.imgLNum = strcat('I1_',num2str(handles.imageNumber),'.png');
handles.imgRNum = strcat('I2_',num2str(handles.imageNumber),'.png');
handles.imageLeft = strcat(handles.directory,handles.imgLNum);
handles.imageRight = strcat(handles.directory,handles.imgRNum);
axes(handles.img1);
imshow(handles.imageLeft);
axes(handles.img2);
imshow(handles.imageRight);
handles.imageNumber = handles.imageNumber+1;
I have a button called "Next" on my GUI and I want to load the next image into the axes when it is clicked. handles.imageLeft and handles.imageRight have the path for the images. When the hit the button the first time, the axes gets updated with the images. But subsequent clicks on the button do not update the axes. But the 'Click Next' text is displayed in console, so I know the callback function is being called.
Thank you #Adiel for the help. I find the problem following your suggestions. I updated the handles.imageLeft and handles.imageRight and I added the code guidata(hObject, handles); I think this function updates the handles (This is my first MATLAB GUI, so I am not very sure). Now the problem is fixed.

Matlab adding KeyListener to existing button

I've written a Matlab program which counts different values when are particular button is pressed (so far just the numbers of "yes" and "no"). I'm trying to add a key listener so that when I press, for example, n on the keyboard the button is pressed, or the same actions are completed. I have tried the addListener and keyfunclistener functions but neither seems to be working.
Here is an example of the button:
no=uicontrol(h_fig,'callback',countnonerve,'position',[.65 .07 .1 .08],'string','No','style','pushbutton','Units','normalized');
Any suggestions? It would be very helpful I'm not familiar with MatLab
You could try using the KeyPressFcn property of the figure to record/capture individual key presses:
function keypress_Test
h = figure;
set(h,'KeyPressFcn',#keyPressCb);
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
end
end
The above code can be pasted into a file and saved as keypress_Test.m. A figure is created and a callback assigned to the KeyPressFcn property.
It can be easily adapted for your GUI. Wherever you have access to the figure handles (probably the h_fig from above) just add set(h_fig,'KeyPressFcn',#keyPressCb); and paste the keyPressCb code into the same file.
If you are counting the number of times a certain key is pressed, then you will have to save this information somewhere..probably to the handles structure. Is that the case? If so, then you can easily access this from the callback
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
% get the handles structure
handles = guidata(src);
if ~isempty(handles)
% do something here - increment count for evnt.Key
% save the updated count
guidata(src,handles);
end
end
Try it out and see what happens!

How can I save the edit box data in the GUI Guide?

I am making a GUI in which there is a multi-line edit box.
the user will have to input the 3 x-y coordinates in this edit box at a time:
[345.567 123.123]
[390.567 178.098]
[378.000 125.987]
by clicking the push button I want these coordinates to be "saved" in the Matlab GUI workspace in the form of a matrix and by clicking another push button "reloaded" from the workspace so that they can be available for future use.
How can I do that?
Can anyone guide me with this? Help would be appreciated!
There are a number of ways to manage data in GUIDE-generated GUIs. The easiest IMO is to use guidata.
For example inside the "Save" push button callback, you would access the edit box string contents, parse as matrix of numbers as save it inside the handles structure.
function pushbuttonSave_Callback(hObject, eventdata, handles)
handles.M = str2num(get(handles.edit1, 'String'));
guidata(hObject, handles);
end
Next in the "load" button, we do the opposite, by loading the matrix from the handles structure, converting it to string, and set the edit box content:
function pushbuttonLoad_Callback(hObject, eventdata, handles)
s = num2str(handles.M, '%.3f %.3f\n');
set(handles.edit1, 'String',s)
end
If you want to export/import the data to/from the "workspace", you can use the ASSIGNIN/EVALIN function:
assignin('base','M',handles.M);
and
handles.M = evalin('base','M');
to save data:
setappdata(h,'name',value)
to load data:
value = getappdata(h,'name')
values = getappdata(h)
where h is the handle you store the data in, name is the variable of the data, value is the actual data.

Matlab checkbox gui

I have a checkbox on GUI that draws a rectangle on a live video feed, however, I need the rectangle to dissapear or be deleted when I uncheck it.
does anyone have any idea how to do this?
This is my code, I have tried putting things in else, but nothing works.
function Box(hObject,eventdata)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
rectangle('Position',[50,50,100,100],'EdgeColor','r')
else
end
You need to save the handle created by the function rectangle. Then add this handle to the big handle of your GUI so that you are able to have access to it once the callback is called again.
So modify your function like so
function Box(hObject,eventdata,handles)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
handles.rectangleSave=rectangle('Position',[50,50,100,100],'EdgeColor','r');
guidata(handles.output,handles);
else
delete(handles.rectangleSave);
end
If you have never used handles, please have a look here :
http://www.matlabtips.com/on-handles-and-the-door-they-open/
handles.output usually stores the handle to the big interface window as explained here :
http://www.matlabtips.com/guide-me-in-the-guide/