How do I create handle variables when initializing a GUI?

5 views (last 30 days)
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
Milos
Milos on 25 Oct 2012
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.
Matt
Matt on 25 Oct 2012
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

Find more on Entering Commands 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!