Gui appears and disappears before user can do anything with it

I have been working on a script and gui for sometime and have now decided to completely restructure my code. I have blocked out large chunks of it until I can move it to its new home. As far as I can tell, the only code left operational in the gui is that which is automatically generated by GUIDE. The exception to this is the code in the uicontrol callbacks, but that should only run once invoked by the user, and a setappdata line in OpeningFcn.
The problem is my gui just appears and immediately disappears. On debugging, I've found that it doesn't even get to OpeningFcn, so must have a problem somewhere before that. Further debugging shows that the gui appears and disappears when the following line from gui_mainfcn.m is run:
gui_hFigure = matlab.hg.internal.openfigLegacy(name, singleton, visible);
This is at line 286 and is the 'else' statement in the function local_openfig.
In addition, weird other events also happen - like I can't close Matlab without it going into debug mode and then getting stuck in a loop. I can only get out if I press the window close cross many times.
I'm afraid this is completely beyond my level of Matlab so I'd be grateful for any help on why this has started happening.
UPDATE: If I comment out the following line from OpeningFcn then it behaves properly:
setappdata(gcbf,'BreakLoop',true);
I'm struggling to understand this if it doesn't even get to OpeningFcn. What's wrong with this line of code?
Regards.

6 Comments

I suspect this line returns an error, because there is no CallBackFigure, so gcbf fails. Apparently all the bloat code added by GUIDE catches the error and then closes the figure without any explanation.
This is one of the reasons I dislike GUIDE so much. Enough to have written a small guide to avoid GUIDE (pun intended):
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(_);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
It would appear that it is the use of gcbf that is the problem. Is this because it hasn't been made visible yet?
If I use the explicit name of the fig then it seems to work.
If this is not a daft question, where is setappdata data actually stored? Having used setappdata I notice that nothing appears in the workspace. Is it visible to me anywhere or can it only be accessed using getappdata?
If you are in the OpeningFcn, what is wrong with using hObject? You never (rarely, perhaps, though I would say never) want to be using things like gcbf in real code when you can just use an explicit handle instead.
gcbf returns the handle of the object whose callback is executing. I don't actually know if OpeningFcn counts as a figure callback, as I've never tried this kind of thing, but just use hObject if it works
appdata is stored on whatever object you give it a handle to. You cannot see it unless you use getappdata. I tend to always use the handles struct and guidata instead, but it amounts to the same thing in most cases.
"..where is setappdata data actually stored?"
Within the graphics object itself (i.e. the figure).
"Is it visible to me anywhere or can it only be accessed using getappdata?"
You might be able to access it using set get, but I would not recommend doing this: the appdata functions neatly encapsulate the field generation and make the storing and retrieval robust.
"Having used setappdata I notice that nothing appears in the workspace."
There are many workspaces, not just the base workspace. Each workspace is independent, and requires variables to be explicitly passed between them (and that is how it should be). You can view other workspaces by using the debugging tools.
OpeningFcn is an invention of GUIDE, not a true callback. I don't know what gcbf would refer to during OpeningFcn.

Sign in to comment.

 Accepted Answer

It is not because the figure is not visible yet, but because the GUI figure is not a callback figure at that point (at the most it will be the current figure, see gcf). Using an explicit handle to your figure in that line of code would be best.

More Answers (0)

Categories

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!