MATLAB: Recall a figure in guide

callbackfigurematlab gui

Hello! I am building an interface with GUI. By using a pushing bottom I open a figure, and by pushing a second one, I add a filter to the figure in another axes. However, Matlab gives me an error in the second function: the figure does not exits. How can I call the figure in the second function by pushing botoom 2? This is my 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)
[filename pathname]=uigetfile({'*.jpg'}, 'File Selector');
fullpathname=strcat(pathname, filename);
image=imread(fullpathname);
figure.handle=imread(image);
set(handles.text2, 'String', pathname);
handles.axes1=imshow(image);
set(handles.axes1, imshow(image));
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
filter=1-image;
axes(handles.axes2);
imshow(filter);
set(handles.axes2, imshow(filter)

Best Answer

maria - if you want to reference the image that was loaded through the first push button, then save it to the handles object. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
[filename pathname]=uigetfile({'*.jpg'}, 'File Selector');
fullpathname=strcat(pathname, filename);
handles.myImage=imread(fullpathname);
guidata(hObject,handles);
% etc.
Note how the image is saved as
handles.myImage=imread(fullpathname);
and then save the updated handles object with
guidata(hObject,handles);
As an aside, do not create local variables named after built-in MATLAB functions like image. Also, it isn't clear to me what the intent is with
figure.handle=imread(image);
set(handles.text2, 'String', pathname);
handles.axes1=imshow(image);
set(handles.axes1, imshow(image));
What is figure.handle? Why are you overwriting handles.axes1 (which presumably is the axes in your GUI) with the handle returned by imshow?
Anyway, in the second push button callback you reference this image as
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'myImage')
filter=1-handles.myImage;
axes(handles.axes2);
imshow(filter);
end