MATLAB: Cellfun for objects

cell arrayfunctionlive script

Hi, I would like to run the following line of code
lhs=(cellfun(@eval,script));
script is a cell array and the content of each cell is a function of elements of a class called myclass. I get the following error message:
myclass output type is not currently implemented
Is there any workaround? Thanks, J.

Best Answer

I suggest replacing
script = {'a+b'; 'c-d'}
with
script = {@(a,b,c,d)(a+b); @(a,b,c,d)(c-d)};
You can do the conversion by doing something awful like:
script = cellfun(@eval, cellfun(@(x)(['@(a,b,c,d)(', x, ')']), script, 'UniformOutput', false), 'UniformOutput', false);
Note this does not require a, b, c, or d to be defined at the time of defining script. Rather, it just requires you to know the maximum number of objects in advance (here I stopped at 4, but you can use as many as you like).
With this version of script you can then define your objects
a = 1; b = 2; c = 3; d = 4;
and run script without eval:
y = cellfun(@(x)(x(a,b,c,d)), script);
Depending on your actual functions, you might need to set the UniformOutput flag to false.