Thread Subject: Matlab Gui - Handles Error

Subject: Matlab Gui - Handles Error

From: Brian

Date: 24 Nov, 2009 15:58:22

Message: 1 of 2

Hello, I'm new to matlab and I'm coding a GUI.

My aim is create a total of two interfaces, setting them all invisible. If button 1 is clicked, half of them are made visible. If button 2, the other half. I set up a function which created all objects and made them all invisible. I set up a button with a callback. I then set up the callback function to set many UIObjects visible. When the button is clicked, I got the error message:

??? Reference to non-existent field 'Text 1'.

It seems as though the objects are not able to be 'recognised' outside their initial function. I tried adding the line:

guidata(hObject,handles)

Which I understand 'updates' the handles structure. I then got the error message:

??? Input argument "hObject" is undefined.

Can anyone give me a clue as to what I'm doing wrong?

Here is an example of how I've coded it ...
  function build_all_objects(hObject, eventdata, handles, varargin)
handles.fig = figure('units','pixels',...
                        'position',[220 150 750 550],...
                       'resize','off',...
                       'name','Untitled',...
                       'numbertitle','off',...
                       'color', [0.925,0.914,0.847],...
                       'menubar','none');

%Creates the pushbutton
handles.Button1=uicontrol('style','pushbutton',...
                        'position',[10 530 200 20],...
                             'string','Button1',...
                           'enable','on',...
                           'callback',{@Callback, handles});

function Callback(hObject, eventdata, handles)
set([handles.Button1],'visible','no')

Any help would be much appreciated.

Subject: Matlab Gui - Handles Error

From: Chris

Date: 11 Jan, 2010 14:29:04

Message: 2 of 2

Hi Brian

I think there may be two problems here. Firstly, try returning the handles structure from your function eg:

function handles = make_all_panels(hObject, eventdata, handles)

       handles.fig1 = uipanel(...some code)

       handles.button = uicontrol('style', 'pushbutton', etc)

       guidata(hObject, handles)

end

Then call your function from your main program like this

handles = make_all_panels(hObject,eventdata,handles)

This my help with ensuring that your new objects are added to the handles structure.

Secondly, your callbacks may be the problem. I found that if you use guide to make an example button then copy and paste the callback it makes, it looks something like this:

'callback', @(hObject,eventdata)myGui('button1_callback',hObject,eventdata,guidata(hObject))

if you use this format, your gui will recognise the hObject and handles structure. Try these.. I was stuck with a similar problem for quite a while.

Hope this helps

Chris

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gui Brian 24 Nov, 2009 10:59:24
error Brian 24 Nov, 2009 10:59:24
tabs Brian 24 Nov, 2009 10:59:24
visible Brian 24 Nov, 2009 10:59:24
handles Brian 24 Nov, 2009 10:59:24
structure Brian 24 Nov, 2009 10:59:24
functions Brian 24 Nov, 2009 10:59:24
rssFeed for this Thread

Contact us at files@mathworks.com