Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLABhas been retired and will not be updated.

字符串事物

随着时间的流逝,使用MATLAB中的文本已经发展。回溯,文本数据被存储在双阵列中,并带有一个内部标志,以表示其本来是文本。然后,我们改变了此表示形式,因此字符阵列是他们自己的类型。我提到earlier我们介绍了细绳datatype to make working with text data more efficient and natural. Let me show you a little more.

Contents

How to Compare Text: the Olden Days

Early on in MATLAB, we used the functionstrcmpto compare strings. A big caveat for many people is thatstrcmpdoes not behave the same way as its C-language counterpart. We then added over time a few more comparison functions:

to allow case-insensitive matches and to constrain the match to at mostncharacters.

让我们现在进行一些比较。首先在字符串的单元阵列上...

cellChars = {'Mercury','金星','地球','火星'}
CellChars = 1×4个小区阵列{'Mercury'} {'Venus'} {'Earth'} {'Mars'}
TF = strcmp(“弗雷德”,cellChars)
TF = 1×4 logical array 0 0 0 0
TF = strcmp('金星',cellChars)
TF = 1×4 logical array 0 1 0 0
tf = strncmp('火星',细胞琴,2)
TF = 1×4逻辑数组0 0 0 1
tf = strncmp('Marvelous',细胞琴,2)
TF = 1×4逻辑数组0 0 0 1
tf = strncmp('Marvelous', cellChars, 4)
TF = 1×4 logical array 0 0 0 0
tf = strcmpi('mars',细胞琴)
TF = 1×4逻辑数组0 0 0 1
tf = strcmpi('mar',细胞琴)
TF = 1×4 logical array 0 0 0 0

更现代,不完全相同

We also introduced分类arrays for cases where limiting the set of string choices was appropriate. When using分类variables, you may use==进行比较。

CATSTR =分类(细胞琴)
catstr = 1×4分类阵列汞金星地球火星
TF ='火星'== catstr
TF = 1×4逻辑数组0 0 0 1

弦比较大约2020年

And now for细绳comparisons.

str = string(cellChars)%或[“水星”,“金星”,“地球”,“火星”]
str = 1×4 string array "Mercury" "Venus" "Earth" "Mars"

我仍然可以使用str*cmp*functions. But we are not restricted to them.

TF = strcmp ('火星',str)
TF = 1×4逻辑数组0 0 0 1

We can now use==和相关的操作员不必担心字符数组可能引起的索引问题。

TF = str ~=“火星”
TF = 1×4 logical array 1 1 1 0

And most recently, we introduced the function火柴.

TF = matches(str,"Earth")
TF = 1×4逻辑数组0 0 1 0

它具有一些不错的功能,可以使字符串阵列非常漂亮。就像寻找地球内部有轨道的行星一样。

TF = matches(str,[“汞”,“金星”)))
TF = 1×4 logical array 1 1 0 0

And I can, of course, ignore case, with code that, to me, appears less cryptic.

TF = matches(str,“地球”,"IgnoreCase",true)
TF = 1×4逻辑数组0 0 1 0

As is true in all of these cases, we can index into the original array with the logical output to extract the relevant item(s).

str(TF)
ans = "Earth"

My Advice: Err on the Side of Code Readability

我还的t touched on performance here, but one of the drivers for the recent细绳datatype is efficiency and performance. We've worked hard to overlay that with functions that make your code highly readable. This makes code maintenance and code transfer go much more smoothly. I tend to favor this over eking out the last fractional second of speed. In the case of strings, you may not even need to make that tradeoff.

采用字符串

Have you seen enough evidence that string are the future for working with textual data in MATLAB? Tell us what you think这里.




Published with MATLAB® R2019b

|