The cause of 'Error while evaluating uicontrol Callback' in GUI window

I have encountered this error in running a GUI window that I have created. From what I have been able to discern online, it is caused by a problem with the handle structure but the piece of code that continues to be identified by matlab as the problem is this:
Error in new_window_2>pushbutton1_Callback (line 190)
set(handles.verticalBin, 'enable', 'on');
I have changed a number of lines in the code but i am not sure how this (or other set(handles, 'enable', '') statements) are causing an error. How might the handles structure be failing to work properly to generate this error?

2 Comments

The handle struct is not the problem. Please post the complete error message. Currently we see only the part, which defines where the problem is, but not what.
Good code does not use globals: "I have never seen MATLAB code where globals were the right thing to do.... Most of the time I have seen globals being used it was a situation where the code author did not understand scoping of variables. Rather than pass variables from one function to another, they were just being made global."
Note that using globals is a programming practice that will make your code very hard to debug. Passing arguments is much more robust, and is the recommended way of passing data between workspaces:

Sign in to comment.

 Accepted Answer

Using globals is a really bad idea. This impedes the debugging, because you cannot find reliable, which code is resposnible for the last change. Do not use such an indirect method, but the handles struct, which is used for sharing other details already.
It is not clear, where the object of the handle handles.verticalBin is created. Are you sure that this field is existing in the handles struct?

11 Comments

Thanks Jan, I thought I had found a clever solution by using the global to allow me to have the else/if statements :D
Is it possible to use the handles structure to a similar end? In that depending on what pushbutton has been pushed, a different command is carried out by the radio buttons?
The object of handles.verticalBin is in the radio button 2 just as the one for horizontal bin is in radio button 1
If you want to have a button which has 'pushed' or 'not pushed' state then use a toggle button rather than a pushbutton. Then you can just test its value directly. Or use checkboxes or radio buttons. A pushbutton works best as a one-time trigger for a callback.
The pushbutton is essentially a one time trigger for a callback. Each different pushbutton opens the same files just one opens the full files, another the file halved in size and the third a quarter size. These are then plotted. The radio buttons then bin data, depending on which push button was pushed
That's my point - the state of the buttons is used later so they aren't a one time call. Of course you could just use a field on handles:
handles.option = 1;
guidata( hObject, handles )
etc
too if you prefer, just don't do it with globals.
I'm not sure what you mean by a field of handles. Do you mean that if, in each pushbutton callback, i were to say:
handles.option = 1;
or, in another pushbutton callback
handles.option = 2
And then later when i need to determine which file to bin I would have an if statement like:
if handles.option = 1
execute code;
This would eliminate the need for globals as long as I declare handles.option in the opening function
I replaced the globals with handles in a way i think should work but there is an error with the equals signs (=) i used.
Essentially, i gave the handles.option handle a different value in each of my pushbutton callbacks (1, 2 & 3) and then in the callback for for the radio buttons i replaced the globals in the if statement with:
if handles.option = 1 % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellfull, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
elseif handles.option = 2 % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellhalf, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
else % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellquarter, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
end
The error is with the equals in handles.option = 1; and handles.option = 2;
= is the assignment operator
== is the equality operator so you need
if handles.option == 1
etc
Thanks Adam, I thought the == meant that the value is exactly equal to 1 and the = was a more rough equality.
This has fixed that error but the error with verticalBin still remains.
Reference to non-existent field 'verticalBin'.
Error in new_window_2>pushbutton4_Callback (line 249)
set(handles.verticalBin, 'enable', 'on');
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in new_window_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)new_window_2('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I'm not sure why verticalBin causes an error while horizontalBin does not when the callbacks are essentially the same. Unless verticalBin is just the first encountered by the code and horizontalBin will also cause an error. If so, I'm still not sure what the error is caused by. The callback simply sums the data and then plots it like the other, pushbutton callbacks. Might this error disappear if I were to move the callbacks for the radio buttons after the callbacks for the pushbutton in the code?
@Aaron:
I thought the == meant that the value is exactly equal to 1 and
the = was a more rough equality.
This is pure guessing. There is no evenidence in the documentation for this idea. And in addition: What would "more rough" be?! How could Matlab guess, if 1.00001 is roughly 1.0 or not?
I recommend not to guess, how Matlab works. This will cause too many troubles.
Your code would be much easier to read, if you enable the auto-indentation. You can apply this afterwards also: Select the code and hit Ctrl-I.
I assume, you destroy the handles struct in any callback. To find the source if the problem, use the debugger. Set a breakpoint in the main function and in all callbacks. Then step through the code line by line and look, where the 'verticalBin' field is removed.
I was from when I first started off learning basic calculations using notation such as ==, =>, etc. I may have carried it over from here or simply misunderstood the operators when beginning
It can be difficult with some of these operators because as a beginner (sometimes as a more experienced user too) it can be hard to know what to type in the help to get to the right documentation page. Searching for just '=' or '==' does not yield anything so it is useful to know the function name equivalents to search e.g.
doc eq
The assignment operator is a tricky one though, I'm not aware it has a function-based equivalent given its nature and I must admit I gave up trying to work out where in the help it is introduced and defined. I'm sure it is somewhere linked from the Matlab introductory pages, but I didn't find it. Thankfully I know how it works so I don't need to find it on this occasion!

Sign in to comment.

More Answers (1)

hi dear friends and professors.
how can i solve this problem?
Error: label vector and instance matrix must be double
Unable to perform assignment because the indices on the left side are not compatible with the size of the rightn side.
Error in main>multisvm (line 991)
models(k) = svmtrain(TrainingSet,G1vAll);
Error in main>pushbutton14_Callback (line 866)
result = multisvm(data_feat,data_label,test_data);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in main (line 18)
gui_mainfcn(gui_State, varargin{:});
Errorin matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)main('pushbutton14_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

1 Comment

Please open a new thread to ask a new question. This is the section for answers to another question.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Asked:

on 24 May 2017

Commented:

Jan
on 18 Dec 2020

Community Treasure Hunt

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

Start Hunting!