MATLAB: How to call a function with a GUI push button

callbackguiguideimage analysis

I would like to create a GUI that performs a set of specified operations (defined in a separate function) on an image chosen by the user. So far, I have been able to use a push-button and the "uigetfile" function to prompt the user to select an image. This image is then displayed onto the GUI axes. How do I link my separate function called "Cell_Recognition.m" to a second GUI push-button that runs the desired operations on a user specified image and displays the updated image to the axes?
Here is the GUI Guide code of the involved sections:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
[bfn, folder] = uigetfile('*.*','Select an Image for Analysis')
Name = fullfile(folder, bfn)
I = imread(Name)
image(I)
guidata(hObject,handles)
The first pushbutton performs as desired and loads the image into the axes
% --- Executes on button press in pushbutton2.two
function pushbutton2_Callback(hObject, eventdata, handles)
Cell_Recognition(I)
How do I run the user specified image through the function Cell_Recognition.m and display the new image to the axes by clicking a second push button?
Thanks in advance!!

Best Answer

Matthew - if your image is being displayed in a specific axes (say named axes1) of the GUI, then you can get that data in the other callback as follows
function pushbutton2_Callback(hObject, eventdata, handles)
% get the image data
I = get(get(handles.axes,'Children'),'CData');
% do the operation on it
Cell_Recognition(I);
We rely on the fact that the image is the only child on the axes, and so when we call
get(handles.axes,'Children')
it just returns the graphics handle to the image object. We then get the original image data that is stored in CData.
Try the above and see what happens!