0
votes

I have question about using "handles" in Matlab Callback function. I don t know how to use the same thing twice. Please help me.

So,I build Matlab GUI and I have callback function for upload image:

function pushbutton2_Callback(hObject, eventdata, handles)

handles = guidata(hObject); 
[filename pathname]=uigetfile({'*.jpg';'*.bmp'},'File Selector'); 
image=strcat(pathname, filename)
handles.data1=imread(image)
axes(handles.axes1);
imshow(handles.data1);
set(handles.edit1,'string',filename)
set(handles.edit2,'string',pathname)
guidata(hObject, handles);

,and I have callback fuction for converting the same image to "Gray Scale":

function Gray_Callback(hObject, eventdata, handles)

handles = guidata(hObject); 
axes(handles.axes2);
img=handles.data1;
x=imread(img);

y=rgb2gray(x); %function to convert an rgb image to gray scale

imshow (y)
guidata(hObject, handles);

,but it doesn't work.

Does anyone know what I'm doing wrong?

1
“It doesn’t work” is not useful. Please describe what happens and what you expect to happen. Also include here how you link these callbacks to GUI elements and so forth. See How to Ask and minimal reproducible example. - Cris Luengo
Sorry...Uploading image works, i pressed the button and uploaded image, but when I press "Gray Scale" button it doesn t work. So, I think that lines img=handles.data1; x=imread(img); in Gray_Callback function don t work anything..Is it better now? - Anđela Marinović

1 Answers

3
votes

Your first function says

handles.data1=imread(image)

Then your second function says

img=handles.data1;
x=imread(img);

Since img contains image data, not the name of a file, what does imread(img) mean?

I presume you want to work directly with the image data img here, not use imread at all.