フィルターのクリア

Passing name-value arguments to a function that calls feval

8 ビュー (過去 30 日間)
Alex Alex
Alex Alex 2022 年 4 月 13 日
コメント済み: Voss 2022 年 4 月 14 日
Hello,
Is it possible to pass name-value arguments to a function that calls feval? If yes, what is the syntax? The problem looks like this:
% I want to pass any number of name-value pairs here
r = frun(@fA,argA1=2)
% ========================================================================
function r = frun(fh,varargin)
% Function that calls feval
r = feval(fh,varargin);
end
% ========================================================================
function resA = fA(namedArgs)
% Test function A
arguments
namedArgs.argA1 (1,1) double = 1
namedArgs.argA2 (1,1) double = 1
namedArgs.argA3 (1,1) double = 1
end
resA = namedArgs.argA1 + namedArgs.argA2 + namedArgs.argA3;
end
Thanks

採用された回答

Voss
Voss 2022 年 4 月 14 日
Use varargin{:} to send the contents of varargin to the feval function as a comma-separated list of arguments.
% I want to pass any number of name-value pairs here
r = frun(@fA,argA1=2)
r = 4
% ========================================================================
function r = frun(fh,varargin)
% Function that calls feval
r = feval(fh,varargin{:});
end
% ========================================================================
function resA = fA(namedArgs)
% Test function A
arguments
namedArgs.argA1 (1,1) double = 1
namedArgs.argA2 (1,1) double = 1
namedArgs.argA3 (1,1) double = 1
end
resA = namedArgs.argA1 + namedArgs.argA2 + namedArgs.argA3;
end
  2 件のコメント
Alex Alex
Alex Alex 2022 年 4 月 14 日
Many thanks! Works as expected
Voss
Voss 2022 年 4 月 14 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by