Why won't my MATLAB GUI code work?

5 views (last 30 days)
Hi,
I am relatively new to MATLAB but have a piece of coursework to complete.
I am trying to create a GUI that will include 2 axis, 1 of which changes when one of 3 buttons is selected the other takes the selected button and added a form of edge detection and shows it in the 2nd axis. I have tried to create a global statement to label each of the images then add an if statement to the popupmenu to show the image selected, but it doesn't seem to work. Can anyone help?
  1 Comment
Rik
Rik on 1 Apr 2020
I expect Geoff's answer will answer this specific question. If you have further question about GUIs: helpful advice and examples for how to create a GUI can be found in this thread.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 Apr 2020
Ellen - I'm guessing the error has something to do with the image_selected variable not being defined when the popup menu callback fires. This is (most likely) due to the fact that this callback is missing a global image_selected statement in the first line of the callback. So you can add in this mising line in or you can use the handles structure to manage this variable and any other that you might want to share between callbacks. Something like
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.img_selected = 2
axes(handles.axes1)
imshow('policeimage2.jpg');
guidata(hObject,handles); % <---- saves the updated handles object
We call guidata so that the updated handles object is "saved" and so it's updated fields will be available to the other callbacks. In the popup menu callback, you would then do
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
str = get(hObject,'String')
val = get(hObject,'Value')
switch str{val}
case 'Sobel'
if handles.img_selected == 1
% etc.
Note how we access this field via handles.
Or you can do something else. It looks like your code only uses img_selected to know which of the three images you have read and shown on the axes. Rather than reading the file again, you could grab the image data by saving the handle to the image object that is shown on the axes. If you make the following change to each of your three pushbutton callbacks
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1)
handles.hImage = imshow('policeimage2.jpg');
guidata(hObject,handles); % <---- saves the updated handles object
So now your handles structure has a handle to the image graphics object that is shown in axes1. Now in your popup menu callback, you can use this handle to get the image data:
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
str = get(hObject,'String')
val = get(hObject,'Value')
img = get(handles.hImage, 'CData');
switch str{val}
case 'Sobel'
img1 = rgb2gray(img);
img2 = edge(img1,'Sobel')
axes(handles.axes2);
imshow(img2);
case 'Canny'
img1 = rgb2gray(img);
img2 = edge(img1,'Canny');
axes(handles.axes2);
imshow(img2);
case 'Log'
img1 = rgb2gray(img);
img2 = edge(img1,'log');
axes(handles.axes2);
imshow(img2);
case 'Prewitt'
img1 = rgb2gray(img);
img2 = edge(img1,'prewitt');
axes(handles.axes2);
imshow(img2);
case 'Own Method'
img = imread('own_edge_det_output.png');
axes(handles.axes2);
imshow(img);
end
You could also remove alot of the duplicated code in the above as the only difference (with the exception of the last case) is the method used in the edge call.
  3 Comments
Geoff Hayes
Geoff Hayes on 2 Apr 2020
Hi Ellen - glad to have been able to help. If you want to use myownmethod, then I recommend changing the function signature so that you can pass in an image and output an image (whatever the result is for the input). So something like
function [imgOut] = my_own_method(imgIn)
% Identifying the Thresholds of the Image
I = rgb2gray(imgIn);
bw = imbinarize(I);
% Remove any object/threshold of object that is less than 30 pixels
bw = bwareaopen(bw,30);
% Close any gaps
se =strel('disk',2);
bw = imclose(bw,se);
% Fill the thresholds in
bw = imfill(bw,'holes');
% Identify the outer boundary of the fill items
[B,L] = bwboundaries(bw,'noholes');
% Fill the inner boundary in and show the image
imgOut = label2rgb(L,@jet,[.5 .5 .5])
Then in your popup menu callback, for this case you would do
case 'Own Method'
img2 = my_own_method(img);
axes(handles.axes2);
imshow(img2);
(or something similar).
Ellen Taylor
Ellen Taylor on 2 Apr 2020
Thats Brilliant, thank you for all your help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!