How do I call a function handle with an vector - rather than a list of arguments

7 次查看(过去 30 天)
Hi,
I was wondering how to call a function handle with an vector of inputs - rather than the list of aguments.
So if I have a function handle which is defined: (I guess it will be clear that I am working on fitting functions here)
fitFunctionHandle = @(a1, wG1, x1,a2, wG2, x2, c, x)(FitGaussianFn(x, [a1, 0, wG1, x1]) +FitGaussianFn(x, [a2, 0, wG2, x2]) + c
And I have an vector of inputs to hand to it
a1Init = 1
wG1Init = 2
x1Init = 3
a2Init = 4
wG2Init = 5
x2Init = 6
a2Init = 7
x = 8
%startPoint = [a1Init, wG1, x1,a2, wG2, x2, c]
inputArray = [a1Init, wG1, x1,a2, wG2, x2, c, x]
How do I call it with the vector startPoint as the input. If I try (for example)
fitFunctionHandle(inputArray)
I get an Error:
Not enough input arguments.
That makes sense since it is expecting 8 inputs rather than a 8 element vector - but I'm wondering whether there is a way of calling it like that. I think for example in python you can convert an array to a list or something.
I'm aware that I could simplify things in this example case where I have a list of known inputs so I could just do:
fitFunctionHandle(a1Init, wG1, x1,a2, wG2, x2, c, x)
or
fitFunctionHandle(inputArray(1),inputArray(2),inputArray(3,inputArray(4), inputArray(5), inputArray(6),inputArray(7),inputArray(8))
BUT! I guess I'm trying to allow for expansion where I don't know how many arguments there would be.
For bonus points (again since I'm working on fitting functions) - It would be cool to know how to call it when x is a vector as well. I suspect that I would be doing something like
arrayfunc(fitFunctionHandle([startPoint,x]), xVector)
Thanks in advance for your help.

回答(1 个)

Stephen23
Stephen23 2019-11-20
编辑:Stephen23 2019-11-20
Easy: first define a cell array:
C = {a1Init, wG1, x1,a2, wG2, x2, c, x};
and then use a comma-separated list:
fitFunctionHandle(C{:})
Read more:
"For bonus points (again since I'm working on fitting functions) - It would be cool to know how to call it when x is a vector as well"
The sizes of the input arguments makes no difference to a comma-separated list. But I suspect that you should be parameterizing your function properly:
  1 个评论
JP  Hadden
JP Hadden 2019-11-26
Thanks for your help.... That got me quite far along the way.... What I came up with in the end was...
inputArgCell=num2cell(options.startPoint);
inputArgCell{length(inputArgCell)+1} = xData;
yDataInitialGuess = fitFunction(inputArgCell{:});
As you say it seems like the fitFunction can handle the startingpoint guesses being each 1x1, and the xData being 1xN. (or maybe Nx1).
I'm not sure I fully understand the parameterizing suggestion.
I am basically attempting to define a multi peak fit function to pass to Matlab's nonlinear fit function. The fit function is sort-of bolted together from several other functions in string format - which is then evaluated as a function handle. Probably there are better ways to do it - but I was trying to make it fairly general - eg to allow different types of fit function, and different numbers of peaks - and this is what I've come up with for now...
If I'm being dumb - and I fully expect there are better ways - then let me know.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by