MATLAB: cell arrays of function handles

In summary: You are trying to define a function that takes a set of equations in a single function handle. However, you are trying to put the function handles into the format accepted by a complicated MATLAB algorithm. This is not possible. You should use cell arrays instead.
  • #1
mikeph
1,235
18
Hello

I am trying to store function handles in an array. First attempt was to do something like the following:

Code:
for i = 1:5
    r(i) = @(x) [grid(i) - x(1)*(x(2) + value(i))];
end

So I would store 5 function handles, each one using the <5x1> vectors "grid" and "value". Result:

Nonscalar arrays of function handles are not allowed; use cell arrays instead.

Next I try preallocating r as a cell (r = cell(1,5)). Result:

Conversion to cell from function_handle is not possible.

Basically I am having a lot of trouble with the cell arrays and function handles and I don't really know enough about them to be able to interpret the error messages as useful information.

Can anyone see what I'm doing wrong?---

Also, I am confused about function handles with more than one argument. Here I am using two (x(1) and x(2)), but when I type feval(r(1),2,3) to evaluate r(1) at x = [2,3] it says "Index exceeds matrix dimensions."!

Eventually I want to define a function handle with arbitrarily large number of arguments, so r(i) will vary for i=1:100 or so, and then a function all = @(x) [r(1); r(2); ... r(100);], to be able to evaluate all these functions at once. But that seems like a long way away right now.

Thanks,
Mike
 
Physics news on Phys.org
  • #2
I'm not quite sure what you want, but this does give something for me:

grid = 1:5;
value= 1:5;

for i = 1:5
r{i} = @(x) [grid(i) - x(1)*(x(2) + value(i))];
end

r{1}([2 3])


note the curly braces after the r
 
  • #3
Thanks, this has given me a good starting point.

I now have a load of function handles stored in an array, but I want to put them all into a single one.

I am trying to manipulate my functions into the format accepted by a complicated MATLAB algorithm which takes a set of equations in a single function handle.

So I naturally try to put these into the function like this:

Code:
r_total = @(x) [r{1}; r{2}; r{3}; r{4}; r{5}];
>> r_total([1 2])
[COLOR="Red"]? Error using ==> vertcat
Nonscalar arrays of function handles are not allowed; use cell arrays
instead.

Error in ==> @(x)[r{1};r{2};r{3};r{4};r{5}][/COLOR]

I would try r_total = {r{1}; r{2}; r{3}; r{4}; r{5}}; but I don't think the code will accept it, it needs [] brackets in all the examples.
 
Last edited:

Related to MATLAB: cell arrays of function handles

1. What is a cell array in MATLAB?

A cell array in MATLAB is a data structure that can hold different types of data, including numbers, strings, and even other variables. It is similar to a regular array, but each element in a cell array can hold any type of data.

2. What is a function handle in MATLAB?

A function handle in MATLAB is a variable that stores the name of a function or a reference to a function. It allows you to pass functions as arguments to other functions, making it easier to work with complex or frequently used functions.

3. How do you create a cell array of function handles in MATLAB?

To create a cell array of function handles in MATLAB, you can use the curly braces {} notation. For example, you can create a cell array of two function handles like this: func_handles = {@sin, @cos}; This will create a cell array with two elements, each of which is a function handle to the sin and cos functions.

4. How do you access and call a function handle from a cell array in MATLAB?

To access and call a function handle from a cell array in MATLAB, you can use the curly braces {} notation and the parentheses () notation. For example, if you have a cell array called func_handles with a function handle to the sin function at index 1, you can call it like this: func_handles{1}(1); This will call the sin function with an input of 1.

5. What are some use cases for cell arrays of function handles in MATLAB?

Cell arrays of function handles are useful for situations where you need to repeatedly use or pass around the same functions. They can also be used in situations where you need to apply a set of functions to the same data, as you can easily iterate through the cell array and apply each function to the data. Additionally, cell arrays of function handles are commonly used in machine learning and optimization algorithms, where you need to pass a cost function or gradient function as an input to the algorithm.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
Back
Top