Not recognising named input argument

2 次查看(过去 30 天)
This is the head of my function:
function results = spectral_coi(seeds, labels, rr_k, d, n)
arguments
seeds cell % seeds for COI clusters
labels (:, 1) uint8 % labels ( x samples)
end
arguments (Repeating)
rr_k double {mustBeNumeric} % input arrays x k ( x samples x timepoints)
end
arguments
d.idx_seed logical {mustBeNumericOrLogical} = true % are seeds as indices (or in physical units)
d.output_type (1, :) string ...
{mustBeMember(d.output_type, {'all', 'phase', 'power', 'spectral'})} ...
= 'all'
d.transform string ...
{mustBeMember(d.transform, {'native', 'fftw3'})}...
= 'native'
d.verbosity uint8 = 0 % provide feedback?
d.z_pvl double = 0.05 % cluster pval
n.time (1, :) double {mustBeNumeric} % timepoints for reference to seeds physical units
n.f_s double {mustBePositive} % sampling frequency, return normalized frequencies if not provided
n.lof double {mustBePositive} % lowest frequency to calculate (Hz)
n.hif double {mustBePositive} % highest frequency to calculate (Hz)
n.pl {mustBeInteger} % num permutations
end
I call it like this:
results = spectral_coi({[140, 9]}, stim, dat, "output_type", "phase", ...
"idx_seed", false, ...
"is_fbk", false, ...
"transform", "native", ...
"verbosity", 2, ...
"time", time, "f_s", f_s, "hif", 15, "lof", 2, "pl", 2);
Yet rr_k is now
rr_k =
1x7 cell array
Columns 1 through 3
{1078x399 double} {[NaN]} {[NaN]}
Columns 4 through 7
{[NaN]} {[0]} {[NaN]} {[0]}
Suggesting that it has intepreted the inputs up until "transform" as a repeating input. Instead it should have ofcoursed recognised the named inputs like it did with everything afterwards. What is going on? Why start recognising inputs at "transform"?

采纳的回答

Stephen23
Stephen23 2025-3-6
编辑:Stephen23 2025-3-6
The name "is_fbk" is not defined as a named argument.
Because by definition named arguments must come last AND all inputs between the positional and named arguments are repeating MATLAB concludes that therefore everything from DAT to "is_fbk",false must be the repeating argument. Although you might think that the strings would throw an error with MUSTBENUMERIC, in fact the DOUBLE implicitly converts any input to double, including strings: "class — Class or MATLAB data type specified by name, such as double. The input must be the specified type or a type that can be converted to that type. For example, a function that specifies double accepts values of class single and converts them to double" (bold added).
The documentation states "Validation functions error when the input arguments do not match their conditions", but apparently MATLAB attempts the (implicit) type conversion before checking the input with the validation functions (the documentation phrasing could be improved here).
This implicit conversion is also explained in the documentation:
and advises "To avoid conversion of strings to numeric values, use mustBeA, mustBeFloat, or mustBeNumeric."
In short, there is little point in defining both the class and the validation function:
  • if the array must be one specific class AND you want to allow implicit conversion then then specify the class.
  • if the array can be any numeric class (but should definitely not be anything non-numeric) then specify the validation function.
Defining both only serves to confuse you.

更多回答(0 个)

类别

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

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by