How do I create handle variables when initializing a GUI?

I have created a GUI with guide that has two editable text boxes, two drop-down boxes, and a table with two columns, the first of which is editable. The program takes the value entered in to the first column of the table, and using the values entered/selected from the four other boxes, calculates a value which appears in the second column.
My issue is this:
The program works fine as long as I have gone through each of the text/drop-down boxes and either entered a value or hit 'Enter'. The callback for each box uses, as an example
handles.power = str2num(get(hObject,'String'));
guidata(hObject, handles);
to assign the handle variables. However each of the boxes is pre-populated with a default value and I want to be able to directly enter data into the chart without having to click through each box first. According to the guide demo video, I should be able to assign values to the various handle variables under the OpeningFcn function, i.e.
handles.power = -1.00;
However when I do this and then open the GUI and edit the table I get the error message
Attempt to reference field of non-structure array.
Which olny clears once I click on each box and hit enter. Any help would be greatly appreciated... Thanks!
The error comes from the line in the CellEditCallback function, which begins:
function thicknesstable_CellEditCallback(hObject, eventdata, handles)
% hObject handle to thicknesstable (see GCBO)
% eventdata structure with the following fields (see UITABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
P = handles.power;
Which gives the following error:
Attempt to reference field of non-structure array.
Error in ThicknessCalculator>thicknesstable_CellEditCallback (line
207)
P = handles.power;
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in ThicknessCalculator (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)ThicknessCalculator('thicknesstable_CellEditCallback',hObject,eventdata,guidata(hObject))
Error while evaluating uitable CellEditCallback
Turns out that the reason it wasn't working was that I was initializing the GUI by double-clicking the .fig file, instead of calling the GUI name in the command window. This led to the figure opening but the OpeningFcn not running. Thanks for all your assistance!

8 Comments

Please mention the line, which causes the error message.
Mat, can't you get the values from edit text boxes from within any other function. In your _function thicknesstable_CellEditCallback you can get the value of, say, edit box 1 by str2num(get(handles.editbox1, 'String'))
When I implement that code it gives the same error, saying that I am attempting to reference field of non-structure array. I think the issue is that the value, even though it is appearing in the figure, is not being created until I refresh each box. Do I need to set the value in the power_CreateFcn function?
That is strange. All function callbacks created by GUIDE can get access to handles structure. Function callbacks created by user need to have access to handles structure passed onto them. Can you test your GUI by adding a push button? Let the push button action be displaying a value from one of your edit boxes, so in push button function enter this code
disp(get(handles.edit1, 'String'));
where edit1 is the actual tag of your edit box, and the string will be displayed in the Command window when you press the button. Seems like you have some issues with handles strucutre.
This is what I added:
% --- Executes on button press in pushbutton1.
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)
disp(get(handles.power, 'String'));
which uses the power handle from this function (textbox):
function power_Callback(hObject, eventdata, handles)
% hObject handle to power (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of power as text
% str2double(get(hObject,'String')) returns contents of power as a double
handles.power1 = str2num(get(hObject,'String'));
guidata(hObject, handles);
I get the same error:
Attempt to reference field of non-structure array.
Error in ThicknessCalculator>pushbutton1_Callback (line 254)
disp(get(handles.power, 'String'));
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in ThicknessCalculator (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)ThicknessCalculator('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
What could I have done that would have messed up the handle structure? I have tried pre-assigning the variables in the OpeningFcn function, but when I put "handles" in the code at the end of the OpeningFcn code, it doesn't print to the command window.
% --- Executes just before ThicknessCalculator is made visible.
function ThicknessCalculator_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ThicknessCalculator (see VARARGIN)
% Creates initial data values.
handles.Ri = 1.4223;
handles.calctype1 = 1;
handles.BC1 = 8.4;
handles.power1 = -1.00;
% Choose default command line output for ThicknessCalculator
handles.output = hObject;
handles
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ThicknessCalculator wait for user response (see UIRESUME)
% uiwait(handles.figure1);
Hi, in your previous code, you define handles.power1, and you want to display handle.power
Of course you have to full up your handle structure with your own variables before using the guidata function
if you want to see the content of a variable, you can save it inside a mat file eg : save('handles.mat','-struct','handles') the '-struct' property will automatically extract all the fields from handles struct
Mat, my point is that you do not have to mess around with handles structure, and add fields of type double. In the OpeningFcn, you can set these values as strings handled by edit text box or pull down menu functions using (MATLAB preferred way of doing things):
set(handles.tag, 'String', '1.4223');
If the purpose of an edit text box is to serve as user input box, there needs to be no code added into the edit text box function callback. Other function callbacks simply read out the string from the edit box.
Is there a way to verify that the code is actually executing the OpeningFcn function? I haven't altered the varargout function at all. But after adding the code you indicated above, it still acts like handles is empty. For example, I added the above code into the OpeningFcn portion, then added
handles
to the code as the first line of the CellEditCallback function. When I changed a value, I got the same error message indicating I was attempting to access values that didnt exist, and the command window said
handles =
[]
Which would seem to indicate that the OpeningFcn never ran.

Sign in to comment.

Answers (0)

Categories

Asked:

on 25 Oct 2012

Community Treasure Hunt

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

Start Hunting!