主要内容

matlab.lang.makevalidname

构造有效MATLABidentifiers from input strings

描述

example

N= matlab.lang.makevalidname(S)构建有效的MATLAB®identifiers,N, from input strings,S。这makevalidnamefunction does not guarantee the strings inN是独一无二的。

有效的MATLAB标识符是字母数字(A – Z,A – Z,0-9)和下划线的字符向量,因此第一个字符是字母,字符的长度小于或等于namelengthmax

makevalidnamedeletes any whitespace characters before replacing any characters that are not alphanumerics or underscores. If a whitespace character is followed by a lowercase letter,makevalidname将字母转换为相应的大写字符。

example

N= matlab.lang.makevalidname(S,Name,Value)includes additional options specified by one or moreName,Value配对参数。

example

[N,修改的] = matlab.lang.makeValidName(___)returns a logical array,修改的, indicating modified elements. You can use this syntax with any of the input arguments of the previous syntaxes.

例子

collapse all

S = {'Item_#','Price/Unit','一阶','Contact'};n = matlab.lang.makevalidname(s)
N =1x4 cell{'item__'} {'price_unit'} {'x1storder'} {'contect'}

In the first and second elements,makevalidname取代无效字符(#/), with underscores. In the third element,makevalidnameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

Replace invalid characters with the corresponding hexadecimal representation.

S = {'Item_#','Price/Unit','一阶','Contact'};n = matlab.lang.makevalidname(s,“ReplacementStyle',“十六进制”)
N =1x4 cell{'Item_0x23'} {'Price0x2FUnit'} {'x1stOrder'} {'Contact'}

In the first and second elements,makevalidname取代无效字符(#/), with their hexadecimal representation. In the third element,makevalidnameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

删除无效字符。

n = matlab.lang.makevalidname(s,“ReplacementStyle','delete')
N =1x4 cell{'item_'} {'PriceUnit'} {'x1storder'} {'contect'}

makevalidnamedeleted the invalid characters (#/)。在第三个元素中,makevalidnameappended a prefix because the character vector does not begin with a letter, deleted the empty space, and capitalized the character following the deleted space.

S = {'1stMeasurement','2ndMeasurement','控制'};n = matlab.lang.makevalidname(s,'Prefix','m_')
N =1x3 cell{'m_1stMeasurement'} {'m_2ndMeasurement'} {'Control'}

Only the elements that do not start with a letter are prepended with a prefix.

S = {'a%name','name_1','2_name'};[N, modified] = matlab.lang.makeValidName(S)
N =1x3 cell{'a_name'} {'name_1'} {'x2_name'}
修改=1x3逻辑数组1 0 1

makevalidnamedid not modify the second element.

Input Arguments

collapse all

Input strings, specified as a character vector, cell array of character vectors, or string array.

名称值对参数

Specify optional comma-separated pairs ofName,Value参数。Nameis the argument name and价值是相应的值。Name必须出现在引号中。您可以按任何顺序指定几个名称和值对参数NAME1,Value1,...,Namen,Valuen

例子:“替换风格”,“删除”deletes invalid characters.

Replacement style, specified as'underscore','delete', or“十六进制”。该值控制MATLAB如何替代非掌数字符。对于所有值ReplacementStyle,MATLAB删除空格字符,并在空格之后更改小写字母。

ReplacementStyle价值 描述
'underscore'(default) Replaces all characters that are not alphanumerics or underscores with underscores.'underscore'
“十六进制” Replaces each character that is not an alphanumeric or underscore with its corresponding hexadecimal representation.“十六进制”
'delete' 删除所有不是字母数字或下划线的字符。'delete'

字符的字符前缀是输入的前缀makevalidnamereplaces nonalphanumeric characters, specified as a character vector or string scalar. For example, by default,makevalidnameprefixes characters to an input'*hello'because, after it replaces nonalphanumeric characters, the input does not begin with a letter ('_你好')。However, if you specify a replacement style that deletes nonalphanumeric characters,makevalidname不前缀字符。输入输入后,它替换了非掌数字符后('hello')。

有效的前缀必须符合以下条件。

  • 从一封信开始。

  • 仅包含字母数字字符和下划线。

  • 不是MATLAB关键字。

  • Not be longer than the value ofnamelengthmax

Output Arguments

collapse all

有效的MATLAB标识符,返回为字符向量,字符向量的单元格数组或字符串数​​组。输出具有与输入相同数量的尺寸,S

修改后元素的指示灯,作为逻辑标量或数组返回,并且具有与输入相同数量的尺寸,S。A value of1(真的) indicates thatmakevalidname修改的the input in the corresponding location. A value of0(false) indicates thatmakevalidnamedid not need to modify the input in the corresponding location.

提示

  • 为确保输入值有效且唯一,请使用matlab.lang.makeUniqueStringsaftermatlab.lang.makevalidname

    S = {'my.Name','my_Name','my_Name'};有效values = matlab.lang.lang.makevalidname(s)valluniqueValues = matlab.lang.makeuniquestress(varrevalues,{},。。。namelengthmax)
    validValues = 'my_Name' 'my_Name' 'my_Name' validUniqueValues = 'my_Name' 'my_Name_1' 'my_Name_2'

  • To customize an invalid character replacement, first use functions such asstrrep或者REGEXPrep转换为有效字符。例如,转换'@'字符中Sto'At'usingstrrep(s,'@','at')。然后,使用matlab.lang.makevalidname确保所有字符Sare valid.

在R2014A中引入