Got Questions? Get Answers.
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:
cell arrays of function handles

Subject: cell arrays of function handles

From: Grzegorz Knor

Date: 6 Jun, 2011 11:15:27

Message: 1 of 9

Hi,
suppose that we have cell arrays of function handles, for example:
f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
next define x:
x = 0.5
and evaluate all functions:
[f{1}(t);f{2}(t);f{3}(t)]
Is there a way to call all function simpler, something like:
f(:)(x)
or
functions_evaluate(f,x)
?

best regards
Grzegorz

Subject: cell arrays of function handles

From: Loren Shure

Date: 6 Jun, 2011 11:50:19

Message: 2 of 9

"Grzegorz Knor" <gknor@o2.pl> wrote in message
news:isicsf$7sh$1@newscl01ah.mathworks.com...
> Hi,
> suppose that we have cell arrays of function handles, for example:
> f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
> next define x:
> x = 0.5
> and evaluate all functions:
> [f{1}(t);f{2}(t);f{3}(t)]
> Is there a way to call all function simpler, something like:
> f(:)(x)
> or functions_evaluate(f,x)
> ?

You could use cellfun (but it does have an embedded for loop in it).

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

Subject: cell arrays of function handles

From: Grzegorz Knor

Date: 6 Jun, 2011 13:21:04

Message: 3 of 9

"Loren Shure" <loren.shure@mathworks.com> wrote in message <isif0n$d52$1@newscl01ah.mathworks.com>...
> "Grzegorz Knor" <gknor@o2.pl> wrote in message
> news:isicsf$7sh$1@newscl01ah.mathworks.com...
> > Hi,
> > suppose that we have cell arrays of function handles, for example:
> > f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
> > next define x:
> > x = 0.5
> > and evaluate all functions:
> > [f{1}(t);f{2}(t);f{3}(t)]
> > Is there a way to call all function simpler, something like:
> > f(:)(x)
> > or functions_evaluate(f,x)
> > ?
>
> You could use cellfun (but it does have an embedded for loop in it).
>
> --
> Loren
> http://blogs.mathworks.com/loren/
> http://www.mathworks.com/matlabcentral/

OK, but cellfun apply function to each cell in cell array. My problem is quite different. I have some functions and one argument.

Grzegorz

Subject: cell arrays of function handles

From: Matt J

Date: 6 Jun, 2011 13:36:04

Message: 4 of 9

"Grzegorz Knor" wrote in message <isicsf$7sh$1@newscl01ah.mathworks.com>...
> Hi,
> suppose that we have cell arrays of function handles, for example:
> f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
> next define x:
> x = 0.5
> and evaluate all functions:
> [f{1}(t);f{2}(t);f{3}(t)]
==================


>> x=0.5; cellfun(@(c) c(x),f)

ans =

    0.4794
    0.8776
    0.2500

Subject: cell arrays of function handles

From: Steven_Lord

Date: 6 Jun, 2011 13:48:22

Message: 5 of 9



"Grzegorz Knor" <gknor@o2.pl> wrote in message
news:isik80$sa3$1@newscl01ah.mathworks.com...
> "Loren Shure" <loren.shure@mathworks.com> wrote in message
> <isif0n$d52$1@newscl01ah.mathworks.com>...
>> "Grzegorz Knor" <gknor@o2.pl> wrote in message
>> news:isicsf$7sh$1@newscl01ah.mathworks.com...
>> > Hi,
>> > suppose that we have cell arrays of function handles, for example:
>> > f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
>> > next define x:
>> > x = 0.5
>> > and evaluate all functions:
>> > [f{1}(t);f{2}(t);f{3}(t)]
>> > Is there a way to call all function simpler, something like:
>> > f(:)(x)
>> > or functions_evaluate(f,x)
>> > ?
>>
>> You could use cellfun (but it does have an embedded for loop in it).
>>
>> --
>> Loren
>> http://blogs.mathworks.com/loren/
>> http://www.mathworks.com/matlabcentral/
>
> OK, but cellfun apply function to each cell in cell array. My problem is
> quite different. I have some functions and one argument.

So the function you want to apply to each cell is "evaluate the function
handle with my desired input" or g in the code below:

f = {@sin, @cos, @(x) x.^2};
x = 0.5;
g = @(myfunction) myfunction(x);
result = cellfun(g, f)

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

Subject: cell arrays of function handles

From: Grzegorz Knor

Date: 6 Jun, 2011 18:19:08

Message: 6 of 9

Thank you Loren , Matt and Steve :)

best regards
Grzegorz

Subject: cell arrays of function handles

From: Gunar Ernis

Date: 8 Sep, 2011 09:08:32

Message: 7 of 9

Hi,

I have a similar problem: I have a cell array with function handles and I want to evaluate that over a certain range. For example:

f = {@(x) 1./cosh(x), @(x) exp(-x.^2)};
ev_f = @(x) cellfun(@(myfun) myfun(x),f);
x = -1:0.1:1;

then ev_f(x) produces the error message:

??? Error using ==> cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.

Error in ==> @(x)cellfun(@(myfun)myfun(x),f)

Is there a way to evaluate the functions for the whole range of x without using a loop?

Best regards

Gunar

Subject: cell arrays of function handles

From: Steven_Lord

Date: 9 Sep, 2011 01:30:11

Message: 8 of 9



"Gunar Ernis" <gunar.ernis@googlemail.com> wrote in message
news:d553f206-64e1-4eda-ac74-7783199a4598@glegroupsg2000goo.googlegroups.com...
> Hi,
>
> I have a similar problem: I have a cell array with function handles and I
> want to evaluate that over a certain range. For example:
>
> f = {@(x) 1./cosh(x), @(x) exp(-x.^2)};
> ev_f = @(x) cellfun(@(myfun) myfun(x),f);
> x = -1:0.1:1;
>
> then ev_f(x) produces the error message:
>
> ??? Error using ==> cellfun
> Non-scalar in Uniform output, at index 1, output 1.
> Set 'UniformOutput' to false.
>
> Error in ==> @(x)cellfun(@(myfun)myfun(x),f)
>
> Is there a way to evaluate the functions for the whole range of x without
> using a loop?

The error message tells you what you need to do, as did I when you emailed
this question to me directly yesterday. Set the 'UniformOutput' option to
false as shown in the examples in the help text and the documentation. The
COV example in the documentation seems closest to what you want to do --
adapt it to your needs.

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

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

Subject: cell arrays of function handles

From: ashish

Date: 6 Oct, 2013 13:51:08

Message: 9 of 9

"Grzegorz Knor" wrote in message <isicsf$7sh$1@newscl01ah.mathworks.com>...
> Hi,
> suppose that we have cell arrays of function handles, for example:
> f = {@(x)sin(x); @(x)cos(x); @(x)x.*x;};
> next define x:
> x = 0.5
> and evaluate all functions:
> [f{1}(t);f{2}(t);f{3}(t)]
> Is there a way to call all function simpler, something like:
> f(:)(x)
> or
> functions_evaluate(f,x)
> ?
>

We could create a structure array of function handles,like this

>> f.a=@sin; f.b=@cos; f.c=@(x)(x^2);
>> structfun(@(x)x(.5),f,'UniformOutput',false)

ans =

    a: 0.4794
    b: 0.8776
    c: 0.2500

I hope it helps.

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