Pass function which has different variable to another function
5 ビュー (過去 30 日間)
古いコメントを表示
Hi all,
I'm trying to write general function to compute Lyapunov exponents and my general function includes several functions as arguments,
L(v, fun, dfun, gs, n) --> in that function arguments fun, dfun and gs are functions
When I want to call that function if fun and dfun have same input argument with L function, there is not any problem. I can do it as,
L(v, @fun, @dfun, @gs, n)
My problem raises if fun and dfun functions have another variables except common input v. (for example, general logistic map includes r parameter in the equation of
f(x,r) =r*x*(1-x)) How can I call function L if argument functions fun and dfun includes some other parameters?
Thanks for all comments and help
2 件のコメント
DGM
2021 年 4 月 4 日
If I get you right, you have a function that typically takes a bunch of function handles as arguments, but sometimes you need to include additional arguments to be passed to those functions when they're called internally.
If that's about right, I'm not sure my advice is good practice. You could always use varargin and configure your input parsing to handle name-value pairs for your input arguments. That way you could optionally specify an argument list for a certain function using a cell array.
採用された回答
DGM
2021 年 4 月 4 日
編集済み: DGM
2021 年 4 月 4 日
There is probably a more accepted method of doing this, but I'm not the guy to ask for best practices. I threw together an example function to sort of show something similar to my suggestion.
function out=testfun(varargin)
myfunhandles={};
optionalargs={};
optargidx=[];
if numel(varargin)>0
k=1;
while k<=numel(varargin)
thisarg=varargin{k};
if isa(thisarg,'function_handle')
myfunhandles=[myfunhandles; {thisarg}];
k=k+1;
if k<=numel(varargin) && iscell(varargin{k})
optionalargs=[optionalargs; varargin{k}];
optargidx=[optargidx size(optionalargs,1)];
k=k+1;
else
optargidx=[optargidx NaN];
end
end
end
end
% show what we parsed
myfunhandles
optionalargs
optargidx
% execute functions with included arguments
for f=1:numel(myfunhandles)
if isnan(optargidx(f))
myfunhandles{f}()
else
myfunhandles{f}(optionalargs{optargidx(f),:})
end
end
Instead of using name-value pairs or something, I just decided that each function handle may be optionally followed by a cell array containing arguments for that function handle. In this example, none of the function handles have any default arguments, but that could be implemented as well.
Calling the test function:
testfun(@times,{[1 2 3] [4 5 6]},@today,@minus,{rand(3) rand(3)})
dumps the following:
myfunhandles =
@times
@today
@minus
optionalargs =
[1x3 double] [1x3 double]
[3x3 double] [3x3 double]
optargidx =
1 NaN 2
ans =
4 10 18
ans =
738250
ans =
0.7765 -0.1891 -0.1429
-0.2067 -0.1656 0.3492
-0.2746 -0.4988 -0.0691
How you manage the function handles inside your main function is up to you. This is just a general idea.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!