Issues with GUI not running properly
Afficher commentaires plus anciens
I have a GUI that I can't get to work properly. When I open the .m file and run it (with the green triangle) it works exactly as planned, but when I open the .fig file it doesn't work, coming up with "Attempt to reference field of non-structure array."
Any ideas as to why the GUI works when used one way, and not another?
Réponses (1)
Yes, guide GUIs need to have the initialization code in the M-file run before the figure will work as you want. It is the nature of the beast.
You know that part of the code in the M-file that says, "DO NOT EDIT" at the beginning? That opens the figure and sets up the uicontrols to work with the callback functions and whatnot. If you merely open the figure, that code is not run and so those things are not set up and so won't work.
This mutual dependence on two separate entities is one of the reasons I started writing my own GUIs and never use guide anymore (unless I am answering questions here ;-)).
3 commentaires
Matt Fig
le 1 Oct 2012
Nick Bell comments:
"Sorry I've got very little experience with GUIs so don't know what you mean by that, what do I need to do to fix it?
If it's any help, if I open the .fig file in GUIDE and run it, it works fine. It's only if I open the .fig file separately it comes up with the error.
Is this not the initialization code:
function varargout = PAScomparison(varargin) %PASCOMPARISON M-file for PAScomparison.fig % PASCOMPARISON, by itself, creates a new PASCOMPARISON or raises the existing % singleton*. % % H = PASCOMPARISON returns the handle to a new PASCOMPARISON or the handle to % the existing singleton*. % % PASCOMPARISON('Property','Value',...) creates a new PASCOMPARISON using the % given property value pairs. Unrecognized properties are passed via % varargin to PAScomparison_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % PASCOMPARISON('CALLBACK') and PASCOMPARISON('CALLBACK',hObject,...) call the % local function named CALLBACK in PASCOMPARISON.M with the given input % arguments. % % *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 PAScomparison
% Last Modified by GUIDE v2.5 01-Oct-2012 17:14:21
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @PAScomparison_OpeningFcn, ... 'gui_OutputFcn', @PAScomparison_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"
What is unclear? As I said, you simply cannot open the figure by itself and expect the GUI to work.
I don't know how to be more clear than that. MATLAB needs to run the code you pasted above to make the figure work correctly. The way you get MATLAB to run the code is to call the M-File name, which opens the figure and gets the GUI ready to go. Yes, you can also done this by pressing the green triangle in guide. But either way, this is how it is done...
Nick Bell
le 1 Oct 2012
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!