Hello,
i'd like to make a function that can be called even if only a part of its input parameters are defined in the call.
I.e.:
Function definition is something along this lines:
function [ret] = fun(a, b, c, d, e)
if ~exist('a','var')
% parameter a does not exist, so default it to something
a = 0;
end
if ~exist('b','var')
...
ret = [a,b,c,d,e];
return;
The function call should work something like this:
test_call_1 = fun(a=1,b=2,d=3,e=4); (here parameter c is not specified)
test_call_2 = fun(e=4); (here only parameter e is specified)
My function is already able to initialize any missing parameters with default values. I know about using "~" to specify that a parameter is skipped. However, this only works for defining the function, not when calling it (as is required here).
Any help is highly appriciated :)

 채택된 답변

Star Strider
Star Strider 2021년 7월 7일

1 개 추천

See the documentation section on Support Variable Number of Inputs for a demonstration of how to do that.

댓글 수: 4

Thank you for your quick answer!
However, I have already come across this section and I believe it does not solve my problem:
The described solution is for a function with variable and/or repeating arguments, like:
f_var(a_1,b_1, ... , a_n, b_n) with calls like f_var(1,2, ..., n)
However, in my case we have a function that uses a fixed amount of inputs where some of the inputs may be skipped. Perhaps this function is a better example:
f_fix[ret](a, b, c, d):
ret = [a, b, c, d];
And calls are like:
f_fix(a) which returns [a, 0, 0, 0]
f_fix(a, d) which returns [a, 0, 0, d]
f_fix(b, c) which returns [0, b, c, 0]
However, calls like f_fix(a,b,c,d,e) would result in an error since this function takes at max 4 inputs.
I’m lost.
As a general rule, the way MATLAB handles missing inputs is to substitute the empty array [] for them in the function call.
So to omit ‘b’:
out = fcn(a,[],c);
although ‘b’ would have to have a default value assigned in the function once it is detectred as missing.
Try that.
EDIT — (7 Jul 2021 at 15:18)
Added this example —
out = fcn(1,[],3)
out = 1×3 cell array
{[1]} {[0]} {[3]}
function out = fcn(varargin)
a = varargin(1);
b = varargin(2);
c = varargin(3);
if isempty(a{:})
a = 0;
end
if isempty(b{:})
b = 0;
end
if isempty(c{:})
c = 0;
end
out = [a,b,c];
end
.
Thank you again for your help :D
I feel your solution is as good as it gets, but for sake of completness ill include my final "draft" as well:
function [ ret ] = fun(a, b, c, d)
default_param = 0;
if ~exist('a','var') || isempty(a)
a = default_param;
end
if ~exist('b','var') || isempty(b)
b = default_param;
end
if ~exist('x','var') || isempty(x)
c = default_param;
end
if ~exist('c','var') || isempty(c)
d = default_param;
end
ret = [a,b,c,d];
return;
As always, my pleasure!
Thank you!
Noted.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by