A quick workaround that comes to mind is to create another anonymous function as a go-between:
fun = @(x,y,z)x.^2+y.^2+z.^2;
funMiddleMan = @(x) fun(x(1),x(2),x(3));
For a large number of arguments, the solution becomes a little more complicated.
My first instinct is to use str2func
in the following manner
nVar = 3;
funMiddleMan = str2func(['@(x)fun(',sprintf('x(%d),',1:nVar-1),'x(',num2str(nVar),'))']);
However, this will not work since str2func
cannot (currently) embed the definition of fun
within a local workspace attached to funMiddleMan
; meaning that calling funMiddleMan
in this manner will generate an "Undefined function 'fun'
" error. The problem can be circumvented by using eval
:
funMiddleMan = eval(['@(x)fun(',sprintf('x(%d),',1:nVar-1),'x(',num2str(nVar),'))']);
which will work since the string is actually evaluated; however, the use of eval
is typically discouraged for a number of reasons and is only presented for completeness (and a quick and dirty way to get the ball rolling).
Another option is to use convert the vector x
into a cell array and use comma-separated list expansion in the following manner:
splay = @(x) fun(x{:});
funMiddleMan = @(x) splay(mat2cell(x(:),ones(1,numel(x)),1));
which is not necessarily optimal but works.