MATLAB: Won’t the MATLAB GUI code work

guiimage processingMATLABmatlab gui

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?

Best Answer

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.
Related Question