フィルターのクリア

Is it possible to have function handles with optional arguments in Matlab

81 ビュー (過去 30 日間)
I have about 25-50 Matlab function handles saved in a cell array so I can evaluate each of them using a for loop. Those function handles were converted from symbolic expression.
However, some of the function handles need 4 arguments, other need 2 arguments and few do not need any argument (they are constants). For example, some them need (x1,x2,y1,y2) as the arguments, some need(x1,x2), some need (y1,y2) and few need nothing.
My question: is it possible to have function handles with optional arguments in Matlab? which means I can give 4 arguments to the all function handles and let them decide if they need all or not. If they don't need some of them, they can just simply ignore them.
Thank you.
  2 件のコメント
Wan Ji
Wan Ji 2021 年 8 月 12 日
New syntax appears in matlab 2021a, it may help solve your problem!
Walter Roberson
Walter Roberson 2021 年 8 月 12 日
No that does not solve the problem. The = form converts to name/value option pairs.

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 12 日

Yes, anonymous functions can use varargin.

Note that matlabFunction will not generate varargin. It does, however, support specifying variable names in 'vars' that do not occur in the expression. It is absolutely fine to say

matlabFunction(y^2, 'vars', [x, y]) in which case the first input will be ignored 
  2 件のコメント
Muhammad Imron
Muhammad Imron 2021 年 8 月 12 日
Thank you. I didn't know vars can be used for this purpose.
Walter Roberson
Walter Roberson 2021 年 8 月 12 日
syms x1 x2 y1 y2
matlabFunction(y1^2 - x2^2, 'vars', [x1, x2, y1, y2])
ans = function_handle with value:
@(x1,x2,y1,y2)-x2.^2+y1.^2
You still need to pass something in those positions, but whatever you pass will be ignored.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2021 年 8 月 12 日
編集済み: Image Analyst 2021 年 8 月 12 日
It's certainly possible with a regular function. See varargin and nargin in the help. Can you use a regular function? You can still get a handle to a regular function just like you can with an anonymous function.
For an anonymous function, I don't know. I don't think it's possible. If you want to do it for a regular, normal function, let me know.
  1 件のコメント
Muhammad Imron
Muhammad Imron 2021 年 8 月 12 日
Yes, I know to handle this with a regular function. I could do this with regular functions but that would be tedious since I have many functions at hand (can be more than 25 functions). Thank you anyway.

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


Chunru
Chunru 2021 年 8 月 12 日
With anonymous function, you can take in the argument in workspace instead of function arguments.
a = 1; b = 2; c = 1;
f = @(x) a*x.^2 + b*x + c;
y = f(2) % one function arguments; a b c are in workspace
y = 9
  1 件のコメント
Image Analyst
Image Analyst 2021 年 8 月 12 日
I think what he was thinking regarding optional arguments is for you to not pass them in or specify them, and for those variables to take on default values that the function author decided upon in advance.
a = 1; b = 2;
f = @(x, c) a*x.^2 + b*x + c;
% Call without specifying c and having it use some default value for c.
% Case 1: arguments a & b are in workspace. Use (for example) 9 for c instead of 1.
y = f(2) % Won't work - c not specified
But like you pointed out, the way to do that is to just specify the value you want for c in advance:
% Case 2: arguments a, b, & c are in workspace. Use 1 for c since that is what we specified.
c = 1;
y = f(2, c) % Works since the "default" arguments a, b, and c are in the workspace.
% or even declare f() with no c at all and don't pass in anything.
f = @(x) a*x.^2 + b*x + c; % Use c value from workspace.
y = f(2)
Or of course you could just use a normal function instead of an anonymous one (which is what I'd do).

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by