Attempt to reference field of non-structure array.
1 view (last 30 days)
Show older comments
Hello, I want to make a simple GUI. I have two Radiobuttons in an UIpanel. Additionally a pushbutton . After pushing the Button a Messagebox shall appear , depending on the choice of the radiobuttons .
this is my Code. first I proof, which Button is selected and save this
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
if hObject == handles.radiobutton1
setappdata(handles.radiobutton1,'a',1)
setappdata(handles.radiobutton2,'b',0)
elseif hObject == handles.radiobutton2
setappdata(handles.radiobutton1,'a',0)
setappdata(handles.radiobutton2,'b',1)
end
Depois I get the savefile and make an msgbox, depending on the radiobutton
function pushbutton1_Callback(hObject, eventdata, handles)
a=getappdata(handles.radiobutton1,'a');
b=getappdata(handles.radiobutton2,'b');
if a==1
msgbox ('first button')
else if b==1
msgbox('second button')
end
end
keep running the .m file. Everything is great, but if I start the programm, from the command window I recieve this error.
Attempt to reference field of non-structure array.
- Error in
- untitled>uipanel1_SelectionChangeFcn
- (line 105)
- if hObject == handles.radiobutton1
- Error in gui_mainfcn (line 96)
- feval(varargin{:});
- Error in untitled (line 42)
- gui_mainfcn(gui_State, varargin{:});
- Error in
- @(hObject,eventdata)untitled('uipanel1_SelectionChangeFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
- Error in hgfeval (line 63)
- feval(fcn{1},varargin{:},fcn{2:end});
- Error in
- uitools.uibuttongroup/childAddedCbk>manageButtons
- (line 79)
- hgfeval(cbk, source, evdata);
- Error while evaluating uicontrol Callback
I hope someone could help me
Accepted Answer
Image Analyst
on 7 Nov 2013
First of all, you don't need anything in the callback for the radio buttons or uipanel unless you want to do something, like change a static text label or load a popup list or something like that. You don't need it to just set a and b like you did, since you can get those in the pushbutton callback just by calling get(). So clear out your uipanel1_SelectionChangeFcn() callback and then have just this as the pushbutton callback:
function pushbutton1_Callback(hObject, eventdata, handles)
a = get(handles.radiobutton1,'value');
b = get(handles.radiobutton2,'value');
if a == 1
uiwait(msgbox ('first button'));
end
if b == 1
uiwait(msgbox('second button'));
end
end
0 Comments
More Answers (3)
Walter Roberson
on 7 Nov 2013
In the code you show, you setappdata() when there is a selection change, but you have no indication that you initialized the fields. The selection change routine is not necessarily called between the time you create the uipanel and you push the button. For one thing, the radio button might already have selection.
If the radio button does not already have selection, I do not recall that the selection callback for the uipanel will necessarily be called before the button callback. It might, but I would need to research that.
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks 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!