Attempt to reference field of non-structure array.

1 view (last 30 days)
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
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

More Answers (3)

mueller
mueller on 7 Nov 2013
haha Im so sorry I just realized that i have to start the program over the .m file not with the .fig file. All is working fine. I am so sorry but thank you for help

Walter Roberson
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.

mueller
mueller on 7 Nov 2013
Edited: mueller on 7 Nov 2013
Ok, your( 'Image Analyst') Code works. But how do I write this code if i want to change a static field? Sorry by making this example, I haven't thought that this is a difference. But know it's clear. Additionally, by testing your Code. The Gui opens twice, if I open it over the command window.
@ Walter Robinson. Your right, but i set in my gui openen function:
set(handles.normalbutton,'max');
setappdata(handles.normalbutton,'a',1);
_______________________________________________________________________
Making an example . I have ten pushpottons and an staticfield . Depending on the Value of my two radiobuttons . The pushbuttons shall be enabled oder disabled . Besides, the Edit field shall switch between mode A and mode B
this is my code
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
%grouping the buttons
hButtons = [handles.checkbox1, handles.checkbox2, handles.checkbox3, handles.checkbox4, handles.checkbox5, handles.checkbox6, handles.checkbox7, handles.checkbox8, handles.checkbox9, handles.checkbox10 ]
if hObject == handles.pushbutton1
set(handles.staticfield,'String','pushbutton1')
set(hButtons, 'Enable','off');
elseif hObject == handles.pushbutton2
set(handles.staticfield,'String','pushbutton2')
set(hButtons, 'Enable','on');
end
Looking for help :)

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!