Arguments function: Name-value arguments in default values

85 ビュー (過去 30 日間)
Austin Pavlas
Austin Pavlas 2021 年 8 月 11 日
コメント済み: John 2025 年 6 月 18 日 18:36
I am currently using Matlabs argument validation functionality (arguments) to validation an input struct (via conversion to name-value pair). I was wondering if there is any way to set the default value for name-argument pair as the value from a different name-value pair as below. Ideally this would set the default value of <>.B to be equal to <>.A if the value is not provided.
function my_func(nameValueInputStruct)
arguments
nameValueInputStruct.A double = 1
nameValueInputStruct.B double = nameValueInputStruct.A
end
...
end
Matlab throws the error when I try to do this, but was hoping that there is an available work-around. It is important that the input is a name-value pair struct. Thank you.
Use of name-value arguments in default values is not supported.

採用された回答

Shivam Malviya
Shivam Malviya 2023 年 3 月 7 日
Hi Austin,
I see that you are interested in utilizing the "A" name-value pair argument to assign a default value for the "B" name-value pair argument inside the function argument validation block. However, I regret to inform you that this feature is currently unsupported.
To work around this issue, please note that if a value is not provided for a specific name-value pair argument, it will not appear as a field in the name-value pair argument variable. With this in mind, I have created a function which is attached below for your reference and to help you understand this concept better.
function my_func(nameValueInputStruct)
% Argument validation
arguments
nameValueInputStruct.A double = 1
nameValueInputStruct.B double
end
% Call a helper function to parse the input struct
parseNameValueInputStruct(nameValueInputStruct)
end
function parseNameValueInputStruct(nameValueInputStruct)
%parseNameValueInputStruct Parse the nameValueInputStruct and modify it
% accordingly
% If value for argument "B" is not provided
if ~isfield(nameValueInputStruct, "B")
% Set the value of "B" equal to the value of "A"
nameValueInputStruct.B = nameValueInputStruct.A;
end
end
Please refer to the following links for a better understanding;
  • https://in.mathworks.com/help/matlab/ref/isfield.html
  • https://in.mathworks.com/help/matlab/ref/arguments.html
Best regards,
Shivam Malviya
  3 件のコメント
Walter Roberson
Walter Roberson 2025 年 6 月 18 日 16:14
@John Why not just use filesep ?
John
John 2025 年 6 月 18 日 18:36
The example is intended as an easily understood illustration of what I want to do. I know there are easier ways to handle this particular circumstance. Essentially I want the default param_B value to be determined by the param_A value.

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

その他の回答 (1 件)

Markus Leuthold
Markus Leuthold 2023 年 2 月 13 日
Same with this code, which I expect to work, but it fails with the same error message
arguments
opts.A = mustBeMember(opts.A, ["x" "y"])
end
  1 件のコメント
John
John 2025 年 6 月 18 日 12:12
Probably past the time you care about an answer, but the format for lines in an arguments block is:
argName1 (dimensions) class {validators} = defaultValue
So for your example, what you want is something like:
opts.A string {mustBeMember(opts.A, ["x" "y"])} = "x"

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

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by