Matlab: Accessing parts of anonymous functions

76 Views Asked by At

If I define an 2 by 1 vector anonymous function in MATLAB:

F=@(x,p)[a*x(1)*(1-x(1))-x(1)*x(2)-p*(1-exp(-q*x(1)));...
-x(2)+b*x(1)*x(2)*exp(-x(2)/c)];

is there a way for me access just the first and second components of the function?

More generally, if I have a function F = [f1; f2] (where f1 and f2 are component functions) is there a way of expressing individual component functions, f1 and f2, without coding them manually?

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

You might try something like this (for functions, such as your F above, that are of the form "@(...)[...; ...]").

FS = func2str(F);
i1 = find(FS=='[');
i2 = find(FS==';');
i3 = find(FS==']');
F1 = str2func(FS([1:i1-1,i1+1:i2-1]));
F2 = str2func(FS([1:i1-1,i2+1:i3-1]));