0

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?

  • “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 [ask] and [mcve]. – Cris Luengo Jan 20 '19 at 17:26
  • 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ć Jan 20 '19 at 17:53

1 Answers1

3

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120