Hello(adsbygoogle = window.adsbygoogle || []).push({});

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

So I would store 5 function handles, each one using the <5x1> vectors "grid" and "value". Result:Code (Text):for i = 1:5

r(i) = @(x) [grid(i) - x(1)*(x(2) + value(i))];

end

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 Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# MATLAB: cell arrays of function handles

Loading...

Similar Threads - MATLAB cell arrays | Date |
---|---|

Matlab Coding angular spectrum of waves in a fluid | Thursday at 7:30 AM |

How to program this in Matlab | Feb 28, 2018 |

Matlab : Function handle givin array to cell array of function handles | Mar 5, 2014 |

First time with matlab, inputting cell arrays into function? | Oct 10, 2010 |

Combining 2 two-dimensional cell arrays in MATLAB | Mar 9, 2009 |

**Physics Forums - The Fusion of Science and Community**