Arguments function: Name-value arguments in default values
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Markus Leuthold
le 13 Fév 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 commentaire
John
le 18 Juin 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"
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!