Using arguments with optional parameters and repeating parameters

25 次查看(过去 30 天)
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.

采纳的回答

Pratyush
Pratyush 2023-8-4
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 个评论
Brandon
Brandon 2023-8-15
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by