GUI Errors on callback functions

4 visualizzazioni (ultimi 30 giorni)
Cua Domain
Cua Domain il 22 Mar 2021
Risposto: Geoff Hayes il 23 Mar 2021
I just make a UI in matlab, the UI like 2 button and 2 axes,
if i klik button 1 , the image will show in axes 1, and if i want that if i klick button 2 the image will show in axes 2,
what should i do?
this is the code
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Build the complete filename
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
hold off;
imshow(MyImage,'Parent',handles.axes1);
end
handles.output = hObject;
guidata(hObject, handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
%hold off;
if isfield(handles,'MyImage')
axes2(handles.axes);
hold on;
% Convert RGB image to gray scale image
image=rgb2gray(MyImage);
hold off;
imshow(image,'Parent',handles.axes2);
%gaussian filter:
hold on;
Iblur1 = imgaussfilt(image,2);
Iblur2 = imgaussfilt(image,4);
Iblur3 = imgaussfilt(image,8);
Display the original image and all the filtered images.
end

Risposte (1)

Geoff Hayes
Geoff Hayes il 23 Mar 2021
Cua - you haven't posted what the errors are but I do think part of the problem is that the image isn't being properly saved to the handles structure and so you can't access it from the second callback. The image file name is stored as MyImage which may be a little confusing since it isn't an image but the name and path to the image. For the first callback, you could try something like
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Build the complete filename
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
if ~isequal(filename,0) && ~isequal(pathname,0)
msgbox('User selected image sucessfully','sucess','modal');
MyImage = imread(fullfile(pathname, filename));
imshow(MyImage,'Parent',handles.axes1);
handles.MyImage = MyImage;
guidata(hObject, handles);
else
msgbox ('User pressed cancel','failed','modal');
end
Note how we use fullfile to properly concatenate the path name with the file name. We then read the image into the MyImage variable and store that variable in the handles structure. (I've omitted the waits around the message boxes for simplicity.)
Now, your second callback might look like
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
%hold off;
if isfield(handles,'MyImage')
% Convert RGB image to gray scale image
image=rgb2gray(handles.MyImage);
hold off;
imshow(image,'Parent',handles.axes2);
%gaussian filter:
hold on;
Iblur1 = imgaussfilt(image,2);
Iblur2 = imgaussfilt(image,4);
Iblur3 = imgaussfilt(image,8);
% Display the original image and all the filtered images.
end
The above is mostly the same as your original function. I've just removed the lines that sets the axes and calls hold on. But it should work with the exception of where to show the filtered images. Note that if handles has a field named MyImage then to access that image you must use handles.MyImage rather than just call MyImage on its own.

Categorie

Scopri di più su Display Image in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by