How can I share image data between callbacks?

조회 수: 6 (최근 30일)
SAURABH  AGARWAL
SAURABH AGARWAL 2014년 4월 10일
답변: Sven 2014년 4월 10일
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

답변 (1개)

Sven
Sven 2014년 4월 10일
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.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by