Azzera filtri
Azzera filtri

Using arguments with optional parameters and repeating parameters

22 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Pratyush
Pratyush il 4 Ago 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 Commento
Brandon
Brandon il 15 Ago 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!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by