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:
Visibility of user defined Matlab functions when called from a cpp mex file

Subject: Visibility of user defined Matlab functions when called from a cpp mex file

From: Henry

Date: 4 Apr, 2011 17:00:19

Message: 1 of 3

I have a mex file, say foo.cpp (compiled to foo.mexw32). It takes a string parameter which is the name of a function. Say foo('ff'). Then in foo.cpp I use mexCallMATLAB to invoke ff.

It works well when I create a separate file ff.m. But I want more flexibility:
1. function defined in a function. Eg.
function [] = outF (x)
    function [x] = ff (a)
          x = a;
    end

   foo('ff');
end
Then when running outF, Matlab complains
"Undefined function or method 'ff' for input arguments of type 'double'."

2. Lambda calculus. I want to call foo by the name of a function handle. Say I have a function file func_file.m (and foo('func_file') works well). Now I want to run:
ff = @func_file;
foo('ff');
Again, Matlab complains
"Undefined function or method 'ff' for input arguments of type 'double'."

Is there any *neat* solution? I have some messy solutions but I don't appreciate them.

Thanks in advance.

Subject: Visibility of user defined Matlab functions when called from a cpp mex file

From: Steven_Lord

Date: 4 Apr, 2011 17:27:26

Message: 2 of 3



"Henry " <hz.community@gmail.com> wrote in message
news:inctf3$mtt$1@fred.mathworks.com...
> I have a mex file, say foo.cpp (compiled to foo.mexw32). It takes a
> string parameter which is the name of a function. Say foo('ff'). Then in
> foo.cpp I use mexCallMATLAB to invoke ff.
>
> It works well when I create a separate file ff.m. But I want more
> flexibility:
> 1. function defined in a function. Eg.
> function [] = outF (x)
> function [x] = ff (a)
> x = a;
> end
>
> foo('ff');
> end
> Then when running outF, Matlab complains "Undefined function or method
> 'ff' for input arguments of type 'double'."

I strongly encourage you NOT to use string names to call functions for this
type of reason.

> 2. Lambda calculus. I want to call foo by the name of a function handle.
> Say I have a function file func_file.m (and foo('func_file') works well).
> Now I want to run:
> ff = @func_file;
> foo('ff');
> Again, Matlab complains "Undefined function or method 'ff' for input
> arguments of type 'double'."

Yes; you're still passing the STRING 'ff' into the MEX-file as opposed to
the FUNCTION HANDLE ff.

foo(ff);

Use mexCallMATLAB to invoke FEVAL with the function handle as the first
input argument.

http://www.mathworks.com/help/techdoc/ref/feval.html

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

Subject: Visibility of user defined Matlab functions when called from a cpp mex file

From: Henry

Date: 4 Apr, 2011 17:38:05

Message: 3 of 3

Great! It works now. Thanks a lot.
I also found a useful post here:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/107889

"Steven_Lord" <slord@mathworks.com> wrote in message <incv22$ko2$1@fred.mathworks.com>...
>
>
> "Henry " <hz.community@gmail.com> wrote in message
> news:inctf3$mtt$1@fred.mathworks.com...
> > I have a mex file, say foo.cpp (compiled to foo.mexw32). It takes a
> > string parameter which is the name of a function. Say foo('ff'). Then in
> > foo.cpp I use mexCallMATLAB to invoke ff.
> >
> > It works well when I create a separate file ff.m. But I want more
> > flexibility:
> > 1. function defined in a function. Eg.
> > function [] = outF (x)
> > function [x] = ff (a)
> > x = a;
> > end
> >
> > foo('ff');
> > end
> > Then when running outF, Matlab complains "Undefined function or method
> > 'ff' for input arguments of type 'double'."
>
> I strongly encourage you NOT to use string names to call functions for this
> type of reason.
>
> > 2. Lambda calculus. I want to call foo by the name of a function handle.
> > Say I have a function file func_file.m (and foo('func_file') works well).
> > Now I want to run:
> > ff = @func_file;
> > foo('ff');
> > Again, Matlab complains "Undefined function or method 'ff' for input
> > arguments of type 'double'."
>
> Yes; you're still passing the STRING 'ff' into the MEX-file as opposed to
> the FUNCTION HANDLE ff.
>
> foo(ff);
>
> Use mexCallMATLAB to invoke FEVAL with the function handle as the first
> input argument.
>
> http://www.mathworks.com/help/techdoc/ref/feval.html
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Tags for this Thread

No tags are associated with 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