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 4

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 4

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



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

"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];

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