Search code examples
matlabcell-arrayfunction-handle

How to access function handles in a cell array?


rate_arr_cst_1 = @(t) 2*sin(t)+10; 
rate_arr_cst_2 = @(t) 3*sin(2*t)+8;
rate_arr_cst_h = {rate_arr_cst_1, rate_arr_cst_2};

I defined a cell array in such way and try to access in the following way:

i=1;
h = rate_arr_cst_h(i);

but what I get here is still a cell array, meaning i can't use h to evaluate t=0.1.

Your help is much appreciated!


Solution

  • Either use a for loop:

    for ii = 1:numel(rate_arr_cst_h)
        hh(ii) = rate_arr_cst_h{ii}(i);
    end
    

    or you can use cellfun:

    hh = cellfun(@(f) f(i), rate_arr_cst_h);