필터 지우기
필터 지우기

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 CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by