handles.axis not working: cant set an image
7 次查看(过去 30 天)
显示 更早的评论
hello, can anyone please help with this. i am trying to display an image in gui figure in matlab.
function b2_Callback(hObject, eventdata, handles)
% hObject handle to b2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.pleasewait, 'String','Processing Please Wait..');
name=get(handles.edit1,'string');
originalimage=imread(name);
[ similarityValues, euclideanDistances, fileNames]=retrieve(originalimage);
%display first image in i1
path=fileNames{2};
image1=imread(path);
%disp(path)
%imshow(image1)
imshow(image1,'Parent',handles.i1);
%axes(handles.i1);
%imshow(image1);
I tried
axes(handles.i1);
imshow(image1);
it shows the error:
Error using axes
Invalid axes handle
Error in gui>b2_Callback (line 155)
axes(handles.i1);
and
path=fileNames{2};
image1=imread(path);
imshow(image1,'Parent',handles.i1);
it showed the error:
Error using imshow>validateParent (line 352)
HAX must be a valid axes handle.
Error in imshow (line 251)
validateParent(specific_args.Parent)
Error in gui>b2_Callback (line 152)
imshow(image1,'Parent',handles.i1);
although i tried displaying the path and image and they are just fine.
i also tried to comment the retrieve function and just put the originalimage in the axes i1, it worked just fine.
using the two above methods to set images in other callbacks functions worked also fine.
setting handles.pleasewait also is not working. it is changing nothing.
0 个评论
回答(1 个)
Jan
2021-1-26
Remember that the values of handles is fixed at the time, this callback has been defined. So if the fields "pleasewait" and "i1" did not exist at this time, they are not available in the callback:
function CreateGUI
handles.Fig = figure;
handles.Button = uicontrol('Callback', {@myCallback, handles});
handles.Axes = axes; % After defining the callback!
end
function myCallback(ButtonH, EventData, handles)
handles.Axes % ERROR!
end
Prefer the function guidata to handle this dynamically:
function CreateGUI
handles.Fig = figure;
handles.Button = uicontrol('Callback', @myCallback);
handles.Axes = axes; % After defining the callback!
guidata(handles.Fig, handles); % handles struct is stored in the figure
end
function myCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Obtain the current value
end
Now you can change fields of the handles struct in callbacks also:
function myCallback(ButtonH, EventData)
handles = guidata(ButtonH); % Obtain the current value
handles.newValue = 1;
guidata(ButtonH, handles); % Same as guidata(handles.Fig, handles)
end
Afterwards handles.newValue is available in all callbacks, which retrieve the current value by guidata.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!