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:
how to build an array of function handles

Subject: how to build an array of function handles

From: Fang

Date: 5 Sep, 2012 08:55:08

Message: 1 of 6

I need to create an array of function handles. My code is like this:

b=[@(x)x+1, @(x)x+2];

The message is
Error using horzcat
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
why I got this error?

Subject: how to build an array of function handles

From: Richard Crozier

Date: 5 Sep, 2012 09:51:06

Message: 2 of 6

"Fang" wrote in message <k2741c$inu$1@newscl01ah.mathworks.com>...
> I need to create an array of function handles. My code is like this:
>
> b=[@(x)x+1, @(x)x+2];
>
> The message is
> Error using horzcat
> Nonscalar arrays of function handles are not allowed; use cell arrays instead.
> why I got this error?

Because you need to put the handles in a cell array, not a normal array, e.g.

 b={@(x)x+1, @(x)x+2};

doc cell
doc {

for more information on cell arrays

Subject: how to build an array of function handles

From: Steven_Lord

Date: 5 Sep, 2012 13:32:52

Message: 3 of 6



"Fang " <smtuotuo@gmail.com> wrote in message
news:k2741c$inu$1@newscl01ah.mathworks.com...
> I need to create an array of function handles. My code is like this:
>
> b=[@(x)x+1, @(x)x+2];
>
> The message is Error using horzcat
> Nonscalar arrays of function handles are not allowed; use cell arrays
> instead.
> why I got this error?

Because we no longer allow creation of arrays of function handles. We first
announced this change back in release R14SP2:

http://www.mathworks.com/help/techdoc/rn/f22-1009915.html#f22-1008671

We changed the warning to an error a few years later in release R2008b:

http://www.mathworks.com/help/techdoc/rn/brqyzax-1.html#brslecf-1

As Richard said, use a cell array of function handles.

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

Subject: how to build an array of function handles

From: Matt J

Date: 5 Sep, 2012 13:39:08

Message: 4 of 6

"Fang" wrote in message <k2741c$inu$1@newscl01ah.mathworks.com>...
> I need to create an array of function handles. My code is like this:
>
> b=[@(x)x+1, @(x)x+2];
>
> The message is
> Error using horzcat
> Nonscalar arrays of function handles are not allowed; use cell arrays instead.
> why I got this error?
================

Are you sure you really need this? Is it possible that the following single function will do what you want?

b= @(x) [x(1)+1; x(2)+2];

Subject: how to build an array of function handles

From: Pratyaksh Karan

Date: 20 Oct, 2014 17:42:11

Message: 5 of 6

"Richard Crozier" wrote in message <k277aa$t6b$1@newscl01ah.mathworks.com>...
> "Fang" wrote in message <k2741c$inu$1@newscl01ah.mathworks.com>...
> > I need to create an array of function handles. My code is like this:
> >
> > b=[@(x)x+1, @(x)x+2];
> >
> > The message is
> > Error using horzcat
> > Nonscalar arrays of function handles are not allowed; use cell arrays instead.
> > why I got this error?
>
> Because you need to put the handles in a cell array, not a normal array, e.g.
>
> b={@(x)x+1, @(x)x+2};
>
> doc cell
> doc {
>
> for more information on cell arrays

If I have a n-sized array which I want to use as input and I want to define a n-sized array of function handles, each defined about the n-elements of the input array, how do I do it?

For eg:
x = linpace(0,1,n) %array to be used as input
for i = 1:n
sp(i) = @(x) sum(x)*x(i); %desired handle array
end

Subject: how to build an array of function handles

From: Steven Lord

Date: 20 Oct, 2014 21:02:00

Message: 6 of 6


"Pratyaksh Karan" <pratyakshkaran@iitkgp.ac.in> wrote in message
news:m23hhj$2kf$1@newscl01ah.mathworks.com...
> "Richard Crozier" wrote in message
> <k277aa$t6b$1@newscl01ah.mathworks.com>...
>> "Fang" wrote in message <k2741c$inu$1@newscl01ah.mathworks.com>...
>> > I need to create an array of function handles. My code is like this:
>> >
>> > b=[@(x)x+1, @(x)x+2];
>> >
>> > The message is Error using horzcat
>> > Nonscalar arrays of function handles are not allowed; use cell arrays
>> > instead.
>> > why I got this error?
>>
>> Because you need to put the handles in a cell array, not a normal array,
>> e.g.
>>
>> b={@(x)x+1, @(x)x+2};
>>
>> doc cell
>> doc {
>>
>> for more information on cell arrays
>
> If I have a n-sized array which I want to use as input and I want to
> define a n-sized array of function handles, each defined about the
> n-elements of the input array, how do I do it?

You cannot make a regular array of function handles, as the error message
said. This has thrown an error since release R2008b.

http://www.mathworks.com/help/matlab/release-notes-older.html

Prior to that, MATLAB issued a warning when you tried to create such an
array since somewhere around release R14 or R14SP2.

If you want to create a container to store multiple function handles, you
will need to use a cell array, struct array, or object to do so.

> For eg:
> x = linpace(0,1,n) %array to be used as input
> for i = 1:n
> sp(i) = @(x) sum(x)*x(i); %desired handle array

Instead of defining an array of function handles, define a function handle
that accepts two inputs.

sp = @(x, ind) sum(x)*x(ind);

Whenever you would have tried to evaluate the function handle sp(m) for a
vector x, instead evaluate sp(x, m).

--
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