handles.axis not working: cant set an image

7 Ansichten (letzte 30 Tage)
batoul diab
batoul diab am 26 Jan. 2021
Kommentiert: batoul diab am 28 Jan. 2021
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.

Antworten (1)

Jan
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
batoul diab
batoul diab am 28 Jan. 2021
hello.. thank you very much for your answer. i tried to make use of it but no positive result was found.
i actually tried putting the guidata function before the retrieve function and removing everything after the retrieve function,
function b2_Callback(hObject, eventdata, handles)
set(handles.pleasewait,'string','done');
name=get(handles.edit1,'string');
originalimage=imread(name);
imageFolder=get(handles.edit2,'string');
guidata(hObject, handles);
[ similarityValues, euclideanDistances, fileNames]=retrieve(originalimage,name,imageFolder);
it didn't show any errors.and the function ended peacefully.
but when i tried to put it after after the retrieve function (also everything else is removed)
function b2_Callback(hObject, eventdata, handles)
set(handles.pleasewait,'string','done');
name=get(handles.edit1,'string');
originalimage=imread(name);
imageFolder=get(handles.edit2,'string');
[ similarityValues, euclideanDistances, fileNames]=retrieve(originalimage,name,imageFolder);
guidata(hObject, handles);
it show the following error:
Error using guidata (line 87)
H must be the handle to a figure or figure descendent.
anyway, i also tried this bellow before the retrieve function
if ishghandle(handles.i1)
disp('i1 is accessible');
end
if ishghandle(handles.pleasewait)
disp('pleasewait is accessible');
end
it displayed them properly.
while when i tried to display the after the retrieve, it isn't working.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by