How do I create handle variables when initializing a GUI?
Show older comments
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
Jan
on 25 Oct 2012
Please mention the line, which causes the error message.
Milos
on 25 Oct 2012
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'))
Matt
on 25 Oct 2012
Milos
on 25 Oct 2012
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.
Matt
on 25 Oct 2012
Julien
on 25 Oct 2012
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
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
on 25 Oct 2012
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!