How can I share image data between callbacks?

5 views (last 30 days)
SAURABH  AGARWAL
SAURABH AGARWAL on 10 Apr 2014
Answered: Sven on 10 Apr 2014
The code that I am using is :
function pushbutton1_Callback(hObject, eventdata, handles)
handles.pushbutton1 = imread(uigetfile);
axes(handles.axes1);
imshow(handles.pushbutton1);
guidata(hObject, handles);
function pushbutton2_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.y = rgb2gray(imread(handles.pushbutton1));
axes(handles.axes1);
imshow(handles.y);
guidata(hObject,handles);
I need to use the image that I have loaded using pushbutton1 in the callback of pushbutton2. pushbutton2 does the job of converting the image to grayscale. the error that I am getting is :
Error using imread>parse_inputs (line 458)
The filename or url argument must be a string.
Error in imread (line 317)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in simpleplan3>pushbutton2_Callback (line 96)
handles.y = rgb2gray(imread(handles.imagedata.pushbutton1));
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in simpleplan3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)simpleplan3('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

Answers (1)

Sven
Sven on 10 Apr 2014
Hi Saurabh,
This one has a simple solution. Note the call in your first function:
handles.pushbutton1 = imread(uigetfile);
After that function is finished, the contents of handles.pushbutton1 is the image matrix itself, and not the filename to the image. Therefore in the second function where you call:
handles.y = rgb2gray(imread(handles.pushbutton1));
You can simply replace that with:
handles.y = rgb2gray(handles.pushbutton1);
Did that answer your question?
Thanks, Sven.

Community Treasure Hunt

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

Start Hunting!

Translated by