Arguments function: Name-value arguments in default values
Show older comments
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.
Accepted Answer
More Answers (1)
Markus Leuthold
on 13 Feb 2023
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 Comment
John
on 18 Jun 2025
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"
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!