Error with popupmenu using GUIDE

4 views (last 30 days)
Sana Tayyeb
Sana Tayyeb on 24 Nov 2015
Commented: Sana Tayyeb on 25 Nov 2015
Hi. i am using GUIDE to make my GUI where you can access GUI of different functions from an main GUI. i am having a problem when using the popupmenu. i get its value from a pushbutton. i am getting an error everytime i use this code. my code and ans error are as follows. plz tell me what i am doing wrong. thanks
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%hAxes = findobj(gcf, 'type' ,'axes');
% template=null;
% tem=null;
user_Image = getappdata(Main_GUI,'theimage');
handles = guidata(hObject);
% handles.popupmenu1=gco;
tem = get(handles.popupmenu1,'Value');
guidata(hObjects,handles);
tem
%template put name of figure against each value from menu
if tem==1
template=imread('modal_breast_A.jpg');
if tem==2
template=imread('BREASTnormal1pp.jpg');
end
end
SPOT_MATCHING(user_Image,template);
Attempt to reference field of non-structure array. Error in Match_Spots>pushbutton1_Callback (line 136) tem = get(handles.popupmenu1,'Value'); Error in gui_mainfcn (line 96) feval(varargin{:}); Error in Match_Spots (line 42) gui_mainfcn(gui_State, varargin{:}); Error in @(hObject,eventdata)Match_Spots('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating uicontrol Callback

Accepted Answer

Adam
Adam on 24 Nov 2015
Edited: Adam on 24 Nov 2015
handles = guidata(hObject);
and
guidata(hObjects,handles);
are totally unnecessary where you use them. In this case they shouldn't do any harm so far as I can see, e.g. the first line will just get the same handles that you already have being passed into your callback anyway.
However, the fact that you are using them in places they don't need to be used, combined with the error message you are getting suggests to me that you have overwritten the handles structure in some other callback with something else.
Like many many errors, this is trivially verifiable by putting a breakpoint in or selecting the 'Stop on Errors' option from the 'Breakpoints' menu in the editor.
The error message you have, and the line it is on seems to clearly show that at that point of the code 'handles' is not a struct which it should be if you haven't overwritten it.
guidata(hObjects,handles);
should be called when you have added data to the handles structure. It does not need to be called if you just edit a uicomponent and definitely not if you don't even use anything on handles.
In most cases excessive usage of this does not cause harm, but if you get it wrong then it does and the more times you needlessly use it the more opportunities you have to get it wrong and overwrite 'handles'.
handles = guidata(hObject);
should never need to be called in a GUIDE-created callback, only in a callback you have yourself created and passed just the figure handle to rather than the handles structure (the latter you should never do).
--------
As an aside your if statements at the bottom will not work correctly either. You need to end the first if statement before starting the second else the second will never trigger when nested inside the first.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!