Using arguments with optional parameters and repeating parameters

25 vues (au cours des 30 derniers jours)
Brandon
Brandon le 27 Juil 2023
Commenté : Brandon le 15 Août 2023
function argumentTest(opt,repeat1)
arguments (Input)
opt.test1
opt.test2
end
arguments(Input,Repeating)
repeat1
end
end
This code does not work in MATLAB 2023a. It gives me the error "Positional arguments must be defined before name-value arguments.", which is not a valid error. I would think this is possible since opt in this example would not require an actual placement, but could be thrown anywhere in the function arguments. I think it has to do with exactly that, however. The placement of the optional argument could be in the place of a repeating argument, thus there is no way for it to know it is going on internally.

Réponse acceptée

Pratyush
Pratyush le 4 Août 2023
I understant you get a error "Positional arguments must be defined before name-value arguments." for your function. In MATLAB, the error "Positional arguments must be defined before name-value arguments" occurs when you try to define positional arguments (arguments without a name-value pair) after name-value arguments (arguments with a name-value pair). Try swapping the parameters to the function, replace your function signature with:
function argumentTest(repeat1, opt)
This should resolve the error.
  1 commentaire
Brandon
Brandon le 15 Août 2023
Interesting. If I wanted all types of input arguments, then I have to split the input arguments into two parts.
function argumentTest(nonopt,repeat1,opt)
arguments (Input)
nonopt
end
arguments(Input,Repeating)
repeat1
end
arguments (Input)
opt.test1
opt.test2
end
end
Thank you for the help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by