Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI callbacks using guide in R2008a

Subject: GUI callbacks using guide in R2008a

From: ture vp

Date: 15 Apr, 2008 08:50:18

Message: 1 of 8

Hi,

I use GUIDE to make my GUI's. I don't like the automatic
callback generator so I turn this option off. For those
buttons where I need a callback function I type it myself in
the callback tag in the property inspector, for example:

mygui('pushbutton1_Callback',gcbo,[],guidata(gcbo))

and then I create the appropriate function in the m-file.

However in R2008a this doesn't seem to work anymore
(although it is still described like that in the help!).
When I type the callback code given above in the callback
tag of the property inspector it is refused. When I use the
automatic callback generator there is now written something
completely different:

[1x1 function_handle
array]@(hObject,eventdata)mygui('pushbutton1_Callback',hObject,eventdata,guidata(hObject))

Problems:
1. I cannot edit this line
2. I do not want to use the automatic callback generator
because most of my gui objects do not require callback. All
these unnecessary callbacks just overloads the code (also
CreateFcn function are then created automatically for all
objects on the gui panel).

In the help I could not find anything on this (e.g. under
the title "Callback Syntax and Arguments" the old way of
working (see above) is described).

Any useful information on this topic would be highly
appreciated!

Ture

Subject: GUI callbacks using guide in R2008a

From: Stephan Hoffmann

Date: 16 Apr, 2008 07:33:01

Message: 2 of 8

This is a serious bug in Matlab 2008a.
It makes some trouble to change the callbacks.

When you have created a gui, you have to save it and then
you could change the callback in the matlab command window.

h=hgload('yourgui.fig');
f=findall(h,'Tag','desiredUIControl');
set(f,'Callback','yourgui(''desiredUIControl_Callback'')');
hgsave(h,'yourgui.fig);


Best regards
Stephan Hoffmann

"Ture VP" <turevp@gmail.com> wrote in message
<fu1q8a$5lr$1@fred.mathworks.com>...
> Hi,
>
> I use GUIDE to make my GUI's. I don't like the automatic
> callback generator so I turn this option off. For those
> buttons where I need a callback function I type it myself
in
> the callback tag in the property inspector, for example:
>
> mygui('pushbutton1_Callback',gcbo,[],guidata(gcbo))
>
> and then I create the appropriate function in the m-file.
>
> However in R2008a this doesn't seem to work anymore
> (although it is still described like that in the help!).
> When I type the callback code given above in the callback
> tag of the property inspector it is refused. When I use
the
> automatic callback generator there is now written
something
> completely different:
>
> [1x1 function_handle
> array]@(hObject,eventdata)mygui
('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
>
> Problems:
> 1. I cannot edit this line
> 2. I do not want to use the automatic callback generator
> because most of my gui objects do not require callback.
All
> these unnecessary callbacks just overloads the code (also
> CreateFcn function are then created automatically for all
> objects on the gui panel).
>
> In the help I could not find anything on this (e.g. under
> the title "Callback Syntax and Arguments" the old way of
> working (see above) is described).
>
> Any useful information on this topic would be highly
> appreciated!
>
> Ture
>
>

Subject: GUI callbacks using guide in R2008a

From: ImageAnalyst

Date: 17 Apr, 2008 03:09:36

Message: 3 of 8

On Apr 15, 4:50=A0am, "Ture VP" <tur...@gmail.com> wrote:
> Hi,
>
> I use GUIDE to make my GUI's. I don't like the automatic
> callback generator so I turn this option off. For those
> buttons where I need a callback function I type it myself in
> the callback tag in the property inspector, for example:
>
> mygui('pushbutton1_Callback',gcbo,[],guidata(gcbo))
>
> and then I create the appropriate function in the m-file.
>
> However in R2008a this doesn't seem to work anymore
> (although it is still described like that in the help!).
> When I type the callback code given above in the callback
> tag of the property inspector it is refused. When I use the
> automatic callback generator there is now written something
> completely different:
>
> [1x1 function_handle
> array]@(hObject,eventdata)mygui('pushbutton1_Callback',hObject,eventdata,g=
u=ADidata(hObject))
>
> Problems:
> 1. I cannot edit this line
> 2. I do not want to use the automatic callback generator
> because most of my gui objects do not require callback. All
> these unnecessary callbacks just overloads the code (also
> CreateFcn function are then created automatically for all
> objects on the gui panel).
>
> In the help I could not find anything on this (e.g. under
> the title "Callback Syntax and Arguments" the old way of
> working (see above) is described).
>
> Any useful information on this topic would be highly
> appreciated!
>
> Ture

I know what you mean -- all those numerous, empty CreateFcn callbacks
clutter up your code. I know they now have "code folding" where you
can collapse them but you still have the first line of the callback
showing. I wish there were a way to define a region (e.g.
CreationFunctions region) and I could stick all those empty callbacks
in the region and then collapse/fold that. Then even less clutter
would be showing. MATLAB, can you do that? If not, then maybe in the
next release?
Regards,
ImageAnalyst

Subject: GUI callbacks using guide in R2008a

From: Stephan Hoffmann

Date: 17 Apr, 2008 06:33:02

Message: 4 of 8

ImageAnalyst <imageanalyst@mailinator.com> wrote in message
<5d996e33-1e72-4da6-9a01-
6b15f9dae7bc@b64g2000hsa.googlegroups.com>...
> I know what you mean -- all those numerous, empty
CreateFcn callbacks
> clutter up your code. I know they now have "code
folding" where you
> can collapse them but you still have the first line of
the callback
> showing. I wish there were a way to define a region (e.g.
> CreationFunctions region) and I could stick all those
empty callbacks
> in the region and then collapse/fold that. Then even
less clutter
> would be showing. MATLAB, can you do that? If not, then
maybe in the
> next release?
> Regards,
> ImageAnalyst

If you do not need the create functions, you can remove
them.
In 2008a you need to change the callbacks and create
function directly after creating the uicontrol. Afterwards
you have to change them in the command window according my
previous post.


If you want to keep the create function, but you do not
want them in your main script, you can copy them to a new
file named e.g. CreateFunctions.m

In this file you need a header like this:
function varargout=CreateFunctions(varargin)
  h_func=str2func(varargin{1});
  feval(h_func,varargin{2:nargin});

function CreateFcn1(hObject,eventdata,handles)
  ...

In guide you now can use a command like "CreateFunctions
('CreateFcn1',gcbo,[],guidata(gcbo))" for your create
functions.


Best regards,
Stephan Hoffmann

Subject: GUI callbacks using guide in R2008a

From: ImageAnalyst

Date: 18 Apr, 2008 02:47:44

Message: 5 of 8

On Apr 17, 2:33=A0am, "Stephan Hoffmann"
<stephan.limitedhoffmann.KEINS...@de.bosch.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message
>
> <5d996e33-1e72-4da6-9a01-
> 6b15f9dae...@b64g2000hsa.googlegroups.com>...
>
>
>
>
>
> > I know what you mean -- all those numerous, empty
> CreateFcn callbacks
> > clutter up your code. =A0I know they now have "code
> folding" where you
> > can collapse them but you still have the first line of
> the callback
> > showing. =A0I wish there were a way to define a region (e.g.
> > CreationFunctions region) and I could stick all those
> empty callbacks
> > in the region and then collapse/fold that. =A0Then even
> less clutter
> > would be showing. =A0MATLAB, can you do that? =A0If not, then
> maybe in the
> > next release?
> > Regards,
> > ImageAnalyst
>
> If you do not need the create functions, you can remove
> them.
> In 2008a you need to change the callbacks and create
> function directly after creating the uicontrol. Afterwards
> you have to change them in the command window according my
> previous post.
>
> If you want to keep the create function, but you do not
> want them in your main script, you can copy them to a new
> file named e.g. CreateFunctions.m
>
> In this file you need a header like this:
> function varargout=3DCreateFunctions(varargin)
> =A0 h_func=3Dstr2func(varargin{1});
> =A0 feval(h_func,varargin{2:nargin});
>
> function CreateFcn1(hObject,eventdata,handles)
> =A0 ...
>
> In guide you now can use a command like "CreateFunctions
> ('CreateFcn1',gcbo,[],guidata(gcbo))" for your create
> functions.
>
> Best regards,
> Stephan Hoffmann- Hide quoted text -
>
> - Show quoted text -
---------------------------------------------------
Stephan:
Well maybe I'll give that a try. I just remember from R2006 that when
you deleted a control from GUIDE (which left the callbacks in the
code), exited GUIDE, and then you deleted the callbacks from the code,
all hell broke loose (java explosion) when you tried to read it back
into GUIDE. So I'm leary of deleting stuff especially stuff related
to controls that are still there. Hopefully this bug has been fixed.
Thanks for the tip.
ImageAnalyst

Subject: GUI callbacks using guide in R2008a

From: ture vp

Date: 18 Apr, 2008 08:17:01

Message: 6 of 8

"Stephan Hoffmann"
<stephan.limitedhoffmann.KEINSPAM@de.bosch.com> wrote in
message <fu4a3d$cb$1@fred.mathworks.com>...
> This is a serious bug in Matlab 2008a.
> It makes some trouble to change the callbacks.
>
> When you have created a gui, you have to save it and then
> you could change the callback in the matlab command window.
>
> h=hgload('yourgui.fig');
> f=findall(h,'Tag','desiredUIControl');
> set(f,'Callback','yourgui(''desiredUIControl_Callback'')');
> hgsave(h,'yourgui.fig);
>
>
> Best regards
> Stephan Hoffmann

Hi Stephan,

The method you have described works fine ... except that
after a while the callbacks disappear.

So after I have created a uicontrol I save the GUI. Then I
create the callback function according to what you have
suggested. When I then open the GUI again the callback works
fine, also after closing and opening the GUI several times.
Then I continue programming the GUI and at some point all
callbacks from the GUI are gone. I could not figure out yet
what action is triggering this.
Any idea what is going on?

Best regards,

Ture

Subject: GUI callbacks using guide in R2008a

From: mansoor Khan

Date: 20 Oct, 2010 03:20:04

Message: 7 of 8

I am facing the same kind of problem with r2008a

I have tried the proposed solution but some how it didn't work for me. I have evaluated it with four different cases and none of these work for me, can any body help me in this regard:

1.
set(f,'CreateFcn','@(hObject,eventdata)SMotordaten(''InitializeEdit_CreateFcn'',hObject,eventdata,guidata(hObject),''newedit1'')');
-------
Error:

ans =

    @(hObject,eventdata)Guidaten('InitializeEdit_CreateFcn',hObject,eventdata,guidata(hObject),'newedit1')

2.
set(f,'CreateFcn','@Guidaten(''InitializeEdit_CreateFcn'',hObject,eventdata,guidata(hObject),''newedit1'')');
-------
Error:

??? Error using ==>
@Guidaten('InitializeEdit_CreateFcn',hObject,eventdata,guidata(hObject),'newedit1')
Error: Unbalanced or unexpected parenthesis or bracket.

??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn

3.
set(f,'CreateFcn','Guidaten(''InitializeEdit_CreateFcn'',hObject,eventdata,guidata(hObject),''newedit1'')');
-------
Error:

??? Error using ==> struct2handle
Undefined function or variable 'hObject'.

??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn

4.
set(f,'CreateFcn','[1x1 function_handle array] @(hObject,eventdata)Guidaten(''InitializeEdit_CreateFcn'',hObject,eventdata,guidata(hObject),''newedit1'')');
-------
Error:

??? Error using ==> [1x1 function_handle array]
@(hObject,eventdata)Guidaten('InitializeEdit_CreateFcn',hObject,eventdata,guidata(hObject),'newedit1')
Error: Unexpected MATLAB expression.

??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn

Regards
Mansoor

"Ture VP" <turevp@gmail.com> wrote in message <fu9ldt$skl$1@fred.mathworks.com>...
> "Stephan Hoffmann"
> <stephan.limitedhoffmann.KEINSPAM@de.bosch.com> wrote in
> message <fu4a3d$cb$1@fred.mathworks.com>...
> > This is a serious bug in Matlab 2008a.
> > It makes some trouble to change the callbacks.
> >
> > When you have created a gui, you have to save it and then
> > you could change the callback in the matlab command window.
> >
> > h=hgload('yourgui.fig');
> > f=findall(h,'Tag','desiredUIControl');
> > set(f,'Callback','yourgui(''desiredUIControl_Callback'')');
> > hgsave(h,'yourgui.fig);
> >
> >
> > Best regards
> > Stephan Hoffmann
>
> Hi Stephan,
>
> The method you have described works fine ... except that
> after a while the callbacks disappear.
>
> So after I have created a uicontrol I save the GUI. Then I
> create the callback function according to what you have
> suggested. When I then open the GUI again the callback works
> fine, also after closing and opening the GUI several times.
> Then I continue programming the GUI and at some point all
> callbacks from the GUI are gone. I could not figure out yet
> what action is triggering this.
> Any idea what is going on?
>
> Best regards,
>
> Ture
>
>
>
>
>

Subject: GUI callbacks using guide in R2008a

From: Occa O'Connor

Date: 23 Oct, 2010 03:31:03

Message: 8 of 8

G'Day Mansoor.. I think the best way to do it is:

set(f,'CreateFcn',{@(hObject,eventdata)Guidaten('InitializeEdit_CreateFcn',hObject,eventdata,guidata(hObject),'newedit1')});

The differences being that I turned all your " into ', also I made the value assigned to the property CreateFcn a cell array.

Another option to try is (which I think you may have already tried):
set(f,'CreateFcn','@(hObject,eventdata)Guidaten('InitializeEdit_CreateFcn',hObject,eventdata,guidata(hObject),'newedit1')');

I've upgraded from 2008 because of this very reason.

Cheers

Occa

Tags for this Thread

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.

Contact us