MATLAB: Store image in a handle.

callba

I'm creating a GUI with a bunch of call back functions, the idea is to load an image an do a series of image transformation to it, I would like to keep those changes to the image and pass it around from one call back function to the other, I looked around to see if there is a similar case, even though there was plenty I couldn't figure out what's wrong with my code.
if true
% 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)
guidata(hObject, handles);
img = get(handles.edit1,'String');
I = imread(img);
guidata(handles.figure1,I);
imshow(I);
disp(handles.figure1);
% — Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) guidata(hObject, handles); h =findobj(0,'tag','figure1'); imshow(h); end

Best Answer

When you save I using guidata you replace the handles structure with I, and so handles does not exist any more (and hence none of its values or graphics handles exist either). Instead of throwing handles away, you should store your data as a field of the handles structure:
handles.I = imread(img);
guidata(hObject,handles);
This is exactly what all of the guidata examples show, which is why you should read the documentation.