Name=Value in Function Calls
Since R2021a
MATLAB® supports two syntaxes for passing name-value arguments.
plot(x,y,LineWidth=2)
name=value syntax
plot(x,y,"LineWidth",2)
comma-separated syntax
Use the name=value syntax to help identify name-value arguments
for functions and to clearly distinguish names from values in lists of name-value
arguments.
Most functions and methods support both syntaxes, but there are some limitations on
where and how the name=value syntax can be used:
Mixing
name,valueandname=valuesyntaxes: The recommended practice is to use only one syntax in any given function call. However, if you do mixname=valueandname,valuesyntaxes in a single call, allname=valuearguments must appear after thename,valuearguments. For example,plot(x,y,"Color","red",LineWidth=2)is a valid combination, butplot(x,y,Color="red","LineWidth",2)errors.Using positional arguments after name-value arguments: Some functions have positional arguments that appear after name-value arguments. For example, this call to the
verifyEqualmethod uses theRelTolname-value argument, followed by a string input:Using theverifyEqual(testCase,1.5,2,"RelTol",0.1,... "Difference exceeds relative tolerance.")
name=valuesyntax (RelTol=0.1) causes the statement to error. In cases where a positional argument follows name-value arguments, use thename,valuesyntax.Names that are invalid variable names: Name-value arguments with names that are invalid MATLAB variable names cannot be used with the
name=valuesyntax. See Variable Names for more info. For example, a name-value argument like"allow-empty",trueerrors if passed asallow-empty=true. Use thename,valuesyntax in these cases.
Function authors do not need to code differently to support both the
name,value and name=value syntaxes. For
information on using argument validation with name-value arguments, see Validate Name-Value Arguments.