Filter löschen
Filter löschen

Using arguments with optional parameters and repeating parameters

22 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Pratyush
Pratyush am 4 Aug. 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 Kommentar
Brandon
Brandon am 15 Aug. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by