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:
Cannot find Callback function within m-file

Subject: Cannot find Callback function within m-file

From: kees de Kapper

Date: 20 Mar, 2013 13:32:07

Message: 1 of 3

Hi All,

new question. Hope you could help me with this one:

I'm trying to make my MenuItems dynamically and add Callback functions to it:

> FigName ='Hello';
> set(handles.MenuItem, 'Callback', {eval(['@(hObject, handles) ,FigName,'_CallBack','(hObject,eventdata,guidata(hObject))'])});
>
>
And I have created a functionlike this:
> function handles = Hello_CallBack(hObject, eventdata, handles)
> % some code

If I run the code, and select the MenuItem then I will get the following error:
     Undefined function 'Hello_CallBack' for input arguments of type 'struct'.
     Error in gui_mainfcn (line 94)
            [varargout{1:nargout}] = feval(varargin{:});

If I change the callback definition to:
> set(handles.MenuItem, 'Callback', {eval(['@',FigName,'_CallBack'])});

then I will get an error like this:
    Undefined function 'Hello_CallBack' for input arguments of type 'double'.
    Error while evaluating uimenu Callback

I've tried "which" to see whether the function was available, but indeed it wasn't listed there.

Why is the function not visible/available?

Many thanks!

Kind regards,

Kees

Subject: Cannot find Callback function within m-file

From: Steven_Lord

Date: 20 Mar, 2013 13:52:01

Message: 2 of 3



"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
news:kicdon$ejn$1@newscl01ah.mathworks.com...
> Hi All,
>
> new question. Hope you could help me with this one:
>
> I'm trying to make my MenuItems dynamically and add Callback functions to
> it:
>
>> FigName ='Hello';
>> set(handles.MenuItem, 'Callback', {eval(['@(hObject, handles)
>> ,FigName,'_CallBack','(hObject,eventdata,guidata(hObject))'])});

DON'T DO THIS.

Seriously, don't do this. You'll have trouble understanding what this is
doing two weeks from now when you reread the code.

If you MUST do something like this, use STR2FUNC instead. Alternately, use a
switchyard.

% begin switchyardCallback
function switchyardCallback(nameOfCallback, hObj, eventdata)

switch nameOfCallback
    case 'Hello_Callback'
        Hello_Callback(hObj, eventdata);
    case 'Goodbye_Callback'
        Goodbye_Callback(hObj, eventdata);
end

function Hello_Callback(hObj, eventdata)
handles = guidata(hObj);
% do stuff with hObj, handles, eventdata)

function Goodbye_Callback(hObj, eventdata)
% more stuff
% end switchyardCallback

*snip*

> Why is the function not visible/available?

Because Hello_CallBack is a subfunction, correct? The way you're trying to
do this (assuming we add back in the missing single quote in your first
attempt) doesn't appear to create it correctly while that subfunction is in
scope.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Cannot find Callback function within m-file

From: kees de Kapper

Date: 10 Apr, 2013 21:02:05

Message: 3 of 3

Thanx Steven! It really helped!

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