GUIDE handles not created automatically

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

For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Hey guys, I think I figured it out!
The code now runs fine, without ANY changes. I strongly believe this has something to do with gcf, gca, etc. Because some buttons only work, when I click on the axes right before.
Thanks for all the input, now I now further understand how MATLAB GUIs work :) And I don't like it... xD Even though it is caused by poor coding ^^
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.
Yes, but the problem is: The Tag changes, when I do with the program what I wanna do. The empty "axes" has a different tag than the axes, once I filled my data in. I have no idea why, but, tbh, I don't even care at this point xD The program works, and I'll probably never use GUIDE again ^^ Thanks for your tips. Helped me understand this mess better :)

Sign in to comment.

Answers (3)

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

No, that's not it, there is no clear command (or any other variation of it) there :( Actually, I have to double check if I can publish that code, as it's company property. I'll check that and come back to you. Sad thing is, it won't run anyway, as you have to have a specific serial device connected via USB, to even get to the errors :/
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
Dennis on 17 Oct 2022
Edited: Dennis on 17 Oct 2022
Hey, sorry for taking so long to answer :( When putting a breakpoint directly at the beginning of a callback function, handles is "[]" (empty). So something with the call of the callback function is wrong. Where can I edit that? I also checked with my supervisor, he said I can post generic stuff, leaving out all the "logic". I'll post the code as soon as I removed all secret stuff.
I don't know if this will actually help, because I basically removed all logic. If somethings missing, hit me up.
PS: This code was NOT written by me, it's ugly as f..... Was written by an engineer. Be happy you don't have to see all that ugly logic x)
function varargout = erweiterte_Diagnose_Messprogramm(varargin)
% ERWEITERTE_DIAGNOSE_MESSPROGRAMM MATLAB code for erweiterte_Diagnose_Messprogramm.fig
% ERWEITERTE_DIAGNOSE_MESSPROGRAMM, by itself, creates a new ERWEITERTE_DIAGNOSE_MESSPROGRAMM or raises the existing
% singleton*.
%
% H = ERWEITERTE_DIAGNOSE_MESSPROGRAMM returns the handle to a new ERWEITERTE_DIAGNOSE_MESSPROGRAMM or the handle to
% the existing singleton*.
%
% ERWEITERTE_DIAGNOSE_MESSPROGRAMM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ERWEITERTE_DIAGNOSE_MESSPROGRAMM.M with the given input arguments.
%
% ERWEITERTE_DIAGNOSE_MESSPROGRAMM('Property','Value',...) creates a new ERWEITERTE_DIAGNOSE_MESSPROGRAMM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before erweiterte_Diagnose_Messprogramm_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to erweiterte_Diagnose_Messprogramm_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help erweiterte_Diagnose_Messprogramm
% Last Modified by GUIDE v2.5 01-Apr-2016 07:55:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @erweiterte_Diagnose_Messprogramm_OpeningFcn, ...
'gui_OutputFcn', @erweiterte_Diagnose_Messprogramm_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
%Funktionen:
% function erweiterte_Diagnose_Messprogramm_OpeningFcn(hObject, eventdata, handles, varargin)
% function varargout = erweiterte_Diagnose_Messprogramm_OutputFcn(hObject, eventdata, handles)
% function spei_Callback(hObject, eventdata, handles) % Speichert die Daten
% function lad_Callback(hObject, eventdata, handles)
% function raster_Callback(hObject, eventdata, handles)
% function MIN_MAX_Callback(hObject, eventdata, handles)
% function msg=Fehlercode_interpret(Fehlercode,Min_Max)
% function Close_But_Callback(hObject, eventdata, handles)
% function text_output(uc,mat,handles)
% function Kriterienplot(~,Fehlercode,flat,GdL,GdL_off,Hf_d,Re,Resonanz,dat) %Kriterienplot(Fz,Fehlercode,flat,GdL,GdL_off,Hf_d,Re,Resonanz,dat)
% --- Executes just before erweiterte_Diagnose_Messprogramm is made visible.
function erweiterte_Diagnose_Messprogramm_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 erweiterte_Diagnose_Messprogramm (see VARARGIN)
%%% SECRET (add fields to handles)
% Choose default command line output for erweiterte_Diagnose_Messprogramm
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
%%% SECRET
end
handles.text2.String=msg; drawnow
%%% SECRET
% UIWAIT makes erweiterte_Diagnose_Messprogramm wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = erweiterte_Diagnose_Messprogramm_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%% SECRET
% --- Executes on button press in spei.
function spei_Callback(hObject, eventdata, handles) % Speichert die Daten
% hObject handle to spei (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%% SECRET
% --- Executes on button press in lad.
function lad_Callback(hObject, eventdata, handles)
% hObject handle to lad (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%% SECRET
% --- Executes on button press in raster.
function raster_Callback(hObject, eventdata, handles)
% hObject handle to raster (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
grid;
% Hint: get(hObject,'Value') returns toggle state of raster
% --- Executes on button press in MIN_MAX.
function MIN_MAX_Callback(hObject, eventdata, handles)
% hObject handle to MIN_MAX (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of MIN_MAX
%%% SECRET
function msg=Fehlercode_interpret(Fehlercode,Min_Max)
%%% SECRET
% --- Executes on button press in Close_But.
function Close_But_Callback(hObject, eventdata, handles)
% hObject handle to Close_But (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fclose(handles.s_rec);
close(handles.figure1)
delete(handles.figure1)
function text_output(uc,mat,handles)
%%% SECRET
function Kriterienplot(~,Fehlercode,flat,GdL,GdL_off,Hf_d,Re,Resonanz,dat)
%%% SECRET
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.
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.
I don't know if it's ALL callbacks, but it's definitelly more than just Close_But_Callback(). I have the feeling, that the handle and hObject that get passed into the callback function are incorrect. Where are these callback calls located? In the Callback property?
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'.
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

Sign in to comment.

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.
"How do I correctly start a GUI-program, that was made with GUIDE?"
You can either
  1. click the green run triangle when looking at the m-file in MATLAB, or
  2. click the green run triangle when looking at the fig-file in GUIDE, or
  3. 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

Products

Release

R2022b

Asked:

on 13 Oct 2022

Commented:

on 17 Oct 2022

Community Treasure Hunt

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

Start Hunting!