MATLAB Answers

How to call a function with a GUI push button

46 views (last 30 days)
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!!

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 Dec 2014
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!

  3 Comments

Rishab b
Rishab b on 31 Mar 2020
can we import external function created in another file to this?
Geoff Hayes
Geoff Hayes on 31 Mar 2020
Rishab - what do you mean by import external function? Are you asking if you can call a function that exists outside of your GUI? If so, then yes.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!