handles.axis not working: cant set an image
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Jan
am 26 Jan. 2021
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.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!