GUI- "Attempt to reference field of non-structure array." Error with varargout and using red x to close

1 次查看(过去 30 天)
A similar question was already asked but the link to the solution ( http://www.mathworks.com/support/solutions/data/1-193LD.html?solution=1-193LD ) has expired I think because I can't view it so I figured I would ask it again since similar questions don't seem to relate to varargout:
Error using waitfor Error while evaluating uicontrol Callback
Attempt to reference field of non-structure array.
Error in SaveMethod>SaveMethod_OutputFcn (line 207) varargout{1} = handles.output;
Error in gui_mainfcn (line 263) [varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in SaveMethod (line 40) [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
I get the error when I try to close my GUI window with the red x in the corner (as opposed to a back button I usually use to close). I also tried to include a uiwait in my opening and then uiresume in my output functions but I still got the same error.
Any solutions?
P.S. This is my outputfcn: varargout{1} = handles.output; set(handles.figure1,'Visible','off')
  2 个评论
Geoff Hayes
Geoff Hayes 2014-7-10
Megna - the error attempt to reference field of non-structure array in this case means that the handles structure is not considered a structure (it has no fields) and is probably empty. If you were to put a breakpoint at this line and run the GUI, you could see if that were the case.
Have you made any changes to the yourGuiName_OpeningFcn or to the code that initializes the GUI?
How do you run the GUI - from the Command Window or from within GUIDE?
Why, in the yourGuiName_OutputFcn, do you invoke the line
set(handles.figure1,'Visible','off')
It may help to attach your m file and fig file to your question so that someone else can run it and see if they can reproduce your behaviour.
Megna Hari
Megna Hari 2014-7-10
编辑:Megna Hari 2014-7-10
I added the set(handles.... line since I thought it uses that line to close the window. I attached the files but I don't think they could run by themselves (handles.output is a huge structure and the program needs a ton of other files in order to run).
Any advice on the way I wrote my code is welcome - I'm still getting used to using structures/handles/guis.
Also I'm running it from the command window. There are changes in the openingfcn but not in initialization.

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2014-7-10
I launched the GUI (through GUIDE) and observed the same results as you - when I pressed the close button (x), the error message Attempt to reference field of non-structure array appeared from the SaveProcessingMethod_OutputFcn function.
This is occurring because of the last statement in the SaveProcessingMethod_OpeningFcn
waitfor(handles.figure1,'Visible','off');
This line of code blocks execution of (most) statements until the Visible property of figure1 is off. This includes the execution of the SaveProcessingMethod_OutputFcn which won't occur until the figure has been deleted (via pressing the x button). At this point, handles is empty, and the line of code
varargout{1} = handles.output;
fails because handles is no longer a structure.
The easy fix is to remove the waitfor statement from the SaveProcessingMethod_OpeningFcn function. This will allow the SaveProcessingMethod_OutputFcn to execute normally and there shouldn't be any problems when closing the figure/GUI.
That is the fix, but what was the intent behind including the wait (either waitfor or uiwait)?
You also mention how I added the set(handles.... line since I thought it uses that line to close the window. That line of code will no longer make the GUI/figure visible, but it will still be there. If you want to close it (permanently) then use
delete(handles.figure1);
  1 个评论
Megna Hari
Megna Hari 2014-7-10
Thank you so much! I got rid of the waitfor statement and it worked! Your reasoning makes sense too (I don't remember why I included the waitfor so I guess it wasn't that important).

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by