GUIDE handles not created automatically
Show older comments
Hey MATLAB-Community!
I'm a new MATLAB user (started using it at a company) and I'm facing a problem. I was given a project, that once ran fine, but now there is a "Dot indexing is not supported for variables of this type." error. I know where it comes from, when debugging, I can see that handles is "[]", even though it should be a struct. Of course, dot indexing won't work with an empty cell.
I have an .m file and a .fig file, and a hand.mat that loads handles into the workspace. After starting the program again, handles is an empty cell again, tho. When just xecuting the .m file, I get an empty figure window. When opening the .fig file directly, the layout is correct, but whenever I try to interact with it, I get that dot indexing error.
Now, my question is: How do I correctly start a GUI-program, that was made with GUIDE? How do I make sure those handles and callbacks are correctly configured? A general explanation how it SHOULD work would be sufficient... Documentation on this is not that great.
Kind regards
Dennis
EDIT: I can further narrow down the problem. The "handles" struct gets created automatically, but the problem is, that the handle doesn't get passed to the callback functions correctly, because if I check "pause on error" (run button dropdown menu), then I can see, that inside the callback functions workspace, "handles" is empty again.
EDIT 2: I'm starting to turn this from a question to a tutorial xD I tried working around the problem by writing "handles = guihandles" at the beginning of a callback funtion, but that feels wrong and just shifts the problem to the next place... But then, handles isn't empty anymore. It just doesn't have all the fields it should have.
4 Comments
Rik
on 13 Oct 2022
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Dennis
on 17 Oct 2022
Rik
on 17 Oct 2022
gcf is fine (since the GUI figure will be the current figure if you interact with it), although gcbf is safer.
gca should generally be avoided in favor of explicit axes handles, even outside the context of GUIs.
Dennis
on 17 Oct 2022
Answers (3)
Image Analyst
on 13 Oct 2022
1 vote
Chances are you have a "clear" command inside your function that blows it away. Get rid of that.
If you can attach the .fig and .m files, along with any data file(s) needed to run it, then I could probably fix it.
9 Comments
Dennis
on 13 Oct 2022
Image Analyst
on 13 Oct 2022
Edited: Image Analyst
on 13 Oct 2022
At least post the first few lines of a callback function, or a function that you pass handles into. Leave out any secret stuff.
I never do handles = guihandles. Immediately upon entering the callback function, at line 1 before anything happens, set a breakpoint. handles should have a value. It should be a structure, not null or non-existant.
Dennis
on 17 Oct 2022
After removing the fluff GUIDE adds and removing functions that only do secret things, this is what is left (I'm assuming the stray end in the openingFcn actually closes some of the logic):
function varargout = erweiterte_Diagnose_Messprogramm(varargin)
% the standard IO thing by GUIDE was here
function erweiterte_Diagnose_Messprogramm_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
handles.text2.String=msg; drawnow
function raster_Callback(hObject, eventdata, handles)
grid;
function Close_But_Callback(hObject, eventdata, handles)
fclose(handles.s_rec);
close(handles.figure1)
delete(handles.figure1)
So, you see that isn't much to go on.
The only thing I see that could wipe the guidata is in Close_But_Callback, which closes the GUI, so later calls to guidata are not expected.
I would strongly suggest moving this GUI to normal code as I suggest in the thread I linked earlier. Moving the content of the callbacks is a bit tedious, but not too much work, and then you will have full control over where what function interacts with guidata.
Image Analyst
on 17 Oct 2022
This is what my exit button looks like
% --- Executes on button press in btnExit.
function btnExit_Callback(hObject, eventdata, handles)
% hObject handle to btnExit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
% Print informational message so you can look in the command window and see the order of program flow.
fprintf(1, 'Just entered btnExit_Callback...\n');
% Save the current settings out to the .mat file.
SaveUserSettings(handles);
% Cause it to shutdown.
delete(handles.figMainWindow);
% Print informational message so you can look in the command window and see the order of program flow.
fprintf(1, 'Now leaving btnExit_Callback.\n');
catch ME
% Some error happened if you get here.
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s', ...
mfilename, callStackString, ME.message);
WarnUser(errorMessage);
end
Was it ALL callbacks that couldn't see handles? Or just Close_But_Callback()?
The close(handles.figure1) line is actually not needed since calling delete takes care of closing it automatically.
Dennis
on 17 Oct 2022
Image Analyst
on 17 Oct 2022
Edited: Image Analyst
on 17 Oct 2022
There is a callback property in the Property Inspector for each button that gives the name of the function. However if it's getting in at all, the function name is right. Here is an example of one from my .fig file:
@(hObject,eventdata)WipeHeight('btnAnalyze_Callback',hObject,eventdata,guidata(hObject))
where WipeHeight is the name of my main app and btnAnalyze is the name of the pushbutton. Make sure yours looks similar, especially the stuff for hObject and to the right.
And make sure you're always referencing handles, with a final 's', not handle without a final 's'.
Rik
on 17 Oct 2022
They will be in the callback property of the object. But if you don't trust them, put this at the start of your function:
hObject = gcbo; % Get callback object
handles = guidata(hObject); % This will cascade up until it finds a parent figure
Steven Lord
on 13 Oct 2022
1 vote
When opening the .fig file directly, the layout is correct, but whenever I try to interact with it, I get that dot indexing error.
You cannot launch a GUIDE-based GUI by opening the .fig file directly. Doing so bypasses the setup code in the accompanying .m file and that makes the GUI non-functional.
The "handles" struct gets created automatically, but the problem is, that the handle doesn't get passed to the callback functions correctly, because if I check "pause on error" (run button dropdown menu), then I can see, that inside the callback functions workspace, "handles" is empty again.
Did whoever wrote the code define the callback functions using the GUIDE editor or did they include a line of code that sets the callback function at runtime? If the latter I suspect they may have specified the callback function in such a way that it uses a copy of the handles structure as it existed when that line of code was executed, ignoring any changes made afterwards in other callback functions. Usually that type of problem manifests as the handles structure missing one new field, but it could manifest as the struct being empty.
If you can't post the GUI here but you also can't determine the cause of the problem you may want to contact Technical Support directly using the Contact Support link under the Get Support heading at the end of this page and work with them to investigate.
Image Analyst
on 13 Oct 2022
1 vote
"How do I correctly start a GUI-program, that was made with GUIDE?"
You can either
- click the green run triangle when looking at the m-file in MATLAB, or
- click the green run triangle when looking at the fig-file in GUIDE, or
- type the name of the m-file in the Command Window.
But do NOT double click the .fig file in the Current Folder list of filenames.
Categories
Find more on Structures 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!