Thread Subject: checking a function handle

Subject: checking a function handle

From: Mauro

Date: 30 Oct, 2009 14:36:31

Message: 1 of 3

I want to check in an if statement if a function handle is equal to
the identity @(x) 1. Is there any way for me to do that?

Thanks.

Subject: checking a function handle

From: Loren Shure

Date: 30 Oct, 2009 14:53:30

Message: 2 of 3

In article <8e3b51f4-f830-42f3-ab71-3a7b97a643d6
@p35g2000yqh.googlegroups.com>, msmscarlatti@googlemail.com says...
> I want to check in an if statement if a function handle is equal to
> the identity @(x) 1. Is there any way for me to do that?
>
> Thanks.
>

I'll show you how (with an undocumented function), but why do you want
to check this? What if the user writes the identity as @(x) ones(1), or
any other number of ways. This seems possibly ill-conceived.

You could use functions on the fh, and then get the function expression
from there.

x = @(q) 1
s = functions(x)
x =
    @(q)1
s =
     function: '@(q)1'
         type: 'anonymous'
         file: ''
    workspace: {[1x1 struct]}
>> s.function
ans =
@(q)1

--
Loren
http://blogs.mathworks.com/loren

Subject: checking a function handle

From: Yair Altman

Date: 31 Oct, 2009 17:12:02

Message: 3 of 3

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.2554c7f35cb14522989a5a@news.mathworks.com>...
> In article <8e3b51f4-f830-42f3-ab71-3a7b97a643d6
> @p35g2000yqh.googlegroups.com>, msmscarlatti@googlemail.com says...
> > I want to check in an if statement if a function handle is equal to
> > the identity @(x) 1. Is there any way for me to do that?
> >
> > Thanks.
> >
>
> I'll show you how (with an undocumented function), but why do you want
> to check this? What if the user writes the identity as @(x) ones(1), or
> any other number of ways. This seems possibly ill-conceived.
>
> You could use functions on the fh, and then get the function expression
> from there.
>
> x = @(q) 1
> s = functions(x)
> x =
> @(q)1
> s =
> function: '@(q)1'
> type: 'anonymous'
> file: ''
> workspace: {[1x1 struct]}
> >> s.function
> ans =
> @(q)1
>
> --
> Loren
> http://blogs.mathworks.com/loren


Actually, Loren, I believe that @functions is an entirely-documented function: it has both a help section (%matlabroot%\toolbox\matlab\datatypes\functions.m) and a doc-page. It even appears in the online version: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/functions.html . Perhaps it was undocumented in some old release, but it's documented at least since R2008a.

Yair Altman
http://UndocumentedMatlab.com
 

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com