MATLAB: cell arrays of function handles

Click For Summary
SUMMARY

This discussion focuses on the challenges of storing and manipulating function handles in MATLAB using cell arrays. The user, Mike, initially attempts to create a non-scalar array of function handles, which results in an error indicating that cell arrays should be used instead. After preallocating a cell array, he encounters issues with evaluating the function handles due to incorrect indexing. The conversation highlights the correct usage of curly braces for cell arrays and the need to combine function handles into a single handle for further processing.

PREREQUISITES
  • Understanding of MATLAB syntax and data structures
  • Familiarity with function handles in MATLAB
  • Knowledge of cell arrays and their usage in MATLAB
  • Basic experience with MATLAB error messages and debugging
NEXT STEPS
  • Learn about MATLAB cell arrays and their syntax
  • Explore the use of function handles with multiple arguments in MATLAB
  • Investigate the MATLAB function feval for evaluating function handles
  • Study how to combine multiple function handles into a single handle in MATLAB
USEFUL FOR

MATLAB users, particularly those working with function handles and cell arrays, as well as developers looking to optimize their code for complex mathematical operations.

mikeph
Messages
1,229
Reaction score
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
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
 
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])
? 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}]

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:

Similar threads

Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K