error starting a GUI

I have developed a simple gui with radiobuttons. Everything works fine as long as I start the gui from the script and then use it. If I start directly the gui.fig file, I got this rerror message when I start using the GUI:
Struct contents reference from a non-struct array object.
Error in gui>pushbutton1_Callback (line 88)
dataSet = get(handles.errorset_groupbutton.SelectedObject,{'String','Value'})
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)gui('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

 Risposta accettata

Rik
Rik il 10 Dic 2018

1 voto

That .fig file is not the complete GUI, it is only the visible elements. The callback functions it references are not contained in the figure, so when you try to execute a function, that function can't work properly, because the guidata wasn't set correctly.
You have already identified the solution: don't start the GUI by opening the fig, but start it with the function. If you can't keep them apart, don't use a fig, but either click the 'generate code' option in GUIDE, or avoid GUIDE in the first place.
If you want some hints about avoiding GUIDE, I'dd be happy to give you some pointers about what I do (or you can just have a look at my FEX submission to see some examples).

4 Commenti

Luigi
Luigi il 11 Dic 2018
Thanks. Yes it would be great to have info about how to avoid GUIDE.
Rik
Rik il 11 Dic 2018
My small guide to avoid GUIDE:
  • 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.
Luigi
Luigi il 12 Dic 2018
Thanks. It looks tricky. I will work on it. Maybe it is silly but I find no option "generate code" in GUIDE. Any info about it?
Rik
Rik il 12 Dic 2018
I remembered from an older version of Matlab that the button had a text like "generate code", but apparently it is called "export to MATLAB-file" (it can be found in the file dropdown in the GUIDE editor).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Prodotti

Release

R2017a

Richiesto:

il 10 Dic 2018

Commentato:

Rik
il 12 Dic 2018

Community Treasure Hunt

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

Start Hunting!

Translated by