How to support default parameter in MATLAB FUNCTION ?

1,721 ビュー (過去 30 日間)
M.K.H. CHANDAN
M.K.H. CHANDAN 2015 年 5 月 18 日
編集済み: Nicholas Ayres 2023 年 6 月 22 日
1 . As I know , there is no default parameter support on MATLAB FUNCTION like other high level programming language c,c++, Is there any other methodology through which it can be done like other compiler C,C++? or programmer has to do using his own logic through varargin.

採用された回答

Stephen23
Stephen23 2015 年 5 月 18 日
編集済み: Stephen23 2015 年 5 月 18 日
MATLAB is an interpreted language, not a compiled language, so there is no compiler as such.
In any case there is no inbuilt syntax to allow such things as:
function out = fun(val_1=def_val, val_2=def_val2, ...)
But you could use the input parser class, or use one of the methods listed here:
The most common ways of dealing with this are:
  • Use nargin and some switch or if statements to detemrine the default parameters.
  • Use varargin and logical indexing to detect non-specified arguments (e.g. empty).
  • Use key-value pairs (usually with varargin) and strcmpi to detect the keys.
  • Use a scalar structure to hold all options.
Using varargin with nargin or logical indexing is very easy:
function foobar(varargin)
Defaults = {A,B,C...};
Defaults(1:nargin) = varargin;
OR
function foobar(varargin)
Defaults = {A,B,C...};
idx = ~cellfun('isempty',varargin);
Defaults(idx) = varargin(idx);
MATLAB themselves use different methods in different functions. Have a look at the ODE functions to see the structure syntax, many plotting functions use the key-value syntax.
You can easily combine key-value and scalar-structure in one function, see my FEX submission here to see how:
  6 件のコメント
Tong Zhao
Tong Zhao 2022 年 6 月 16 日
Thanks from 2022
Ludwig Behringer
Ludwig Behringer 2022 年 11 月 14 日
nice

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

その他の回答 (5 件)

Jan Siegmund
Jan Siegmund 2020 年 7 月 4 日
編集済み: Jan Siegmund 2020 年 7 月 8 日
The Answers are not up to date. Modern MATLAB supports the arguments Block:
function out = foo(in)
arguments
in(1,1) double {mustBeFinite} = 0;
end
out = in + 1;
end
Be careful with the validation functions though. They are not "is"-functions returning logical, rather they throw an error. Here is a list of predefined ones: https://de.mathworks.com/help/matlab/matlab_prog/argument-validation-functions.html
If the default argument is a class you may want to use <Class>.empty:
arguments
nameValueArgs.BaseFigure matlab.ui.Figure = matlab.ui.Figure.empty;
end
  2 件のコメント
Stephen23
Stephen23 2020 年 7 月 4 日
Note that the arguments syntax was first introduced in R2019b.
Jan Siegmund
Jan Siegmund 2020 年 7 月 4 日
Ah thank you Stephen, I thought it would have existed for longer.

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


Stefan Schuberth
Stefan Schuberth 2023 年 1 月 23 日
編集済み: Stefan Schuberth 2023 年 1 月 23 日
function testFun(a,b)
arguments a=10; end % default value
arguments b(3,3) double {mustBePositive} = 10; end
% arguments a(size1dim,size2dim) varType {validationFunction} = defaultValue; end
disp(a);
end

Ingrid
Ingrid 2015 年 5 月 18 日
in your function you need to check how many parameters have been passed
function myFunction(variable1,variable2, optionalVarChangeDefaultParam)
if nargin > 2
defaultParam = optionalVarChangeDefaultParam;
else
defaultParam = 2;
end
  1 件のコメント
M.K.H. CHANDAN
M.K.H. CHANDAN 2015 年 5 月 18 日
編集済み: M.K.H. CHANDAN 2015 年 11 月 19 日
Yes , It is programmer way , but MATLAB does not support like function myFunction(variable1,variable2,... optionalVarChangeDefaultParam1 =3, ... optionalVarChangeDefaultParam2 =4 , ... optionalVarChangeDefaultParam4 =5)

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


shmuel katz
shmuel katz 2023 年 6 月 8 日
編集済み: shmuel katz 2023 年 6 月 8 日
I don't know if it helps, but if you know the name of the variables you enter, you can try the following:
function myFunction(var1,var2,var3,var4,...)
defaults.var1 = 1;
defaults.var2 = 2;
defaults.var3 = 3;
defaults.var4 = 4;
defaultNames = fieldnames(defaults);
for nInputName = 1:numel(defaultNames)
variableName = defaultNames{nInputName};
if ~exist(variableName,'var')
eval([variableName,'=','defaults.(variableName);']);
end
end
...
...
...
end
  2 件のコメント
Nicholas Ayres
Nicholas Ayres 2023 年 6 月 8 日
Hi.
I would say that this other comment is strictly better:
Argument blocks allow you even to set default values for name-value pairs (do not need to be positional at all).
Although if you WERE to take a non-argumentbloack approach (argument blocks are only a relatively recent addition), personally, I would use nargin, rather than checking if things exist.
If nargin == 2, you know vars 3+ do not exist.
Something like this may suffice.
function myFunction(var1, var2, var3, var4, ...)
if nargin < 1
var1 = 1;
end
if nargin < 2
var2 = 2;
end
if nargin < 3
var3 = 3;
end
...
...
end
Also, there is no reason to use eval here. Eval is slow and unsafe. There is no good reason here to dynamically create variables.
This is your function. You have set defaults. This means that these variables are always needed and you have not the need to automate this code section. It just hurts everybody.
Varargin is also useful, but make sure you know why you're using it.
shmuel katz
shmuel katz 2023 年 6 月 8 日
You are right, but sometimes you need a more readable and concise code. If you have 10 variables for one function it is a very cumbersome solution. This said for the Old MATLAB, the new ones have the argument choice (but you need to keep the order)

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


Captain Karnage
Captain Karnage 2023 年 6 月 21 日
Yet another update. As of R2021a, MATLAB supports name/value arguments using either the old, quoted name, value list syntax or name = value syntax. https://www.mathworks.com/help/matlab/matlab_prog/validate-name-value-arguments.html
  1 件のコメント
Nicholas Ayres
Nicholas Ayres 2023 年 6 月 22 日
編集済み: Nicholas Ayres 2023 年 6 月 22 日
While this is fun information, it is just syntactic sugar. I don't believe it is relevant to the actual question here, which is about default parameters. Would probably do better as a comment on someone else's answer, rather than its own.
(although the link you have provided and not actually discussed is very on-topic)

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

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by