www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CompareSortAndCharacterizeTextExample.m
%% Compare Text % Compare text in character arrays and string arrays in different % ways. Starting in R2016b, you can create string arrays with % the |string| function, and compare them with relational operators. You % also can compare string arrays and character vectors with the |strcmp| % function. You can sort string arrays using the |sort| function, just as % you would sort arrays of any other type. MATLAB(R) also provides functions % to inspect characters in pieces of text. For example, you can determine which % characters in a character vector or string array are letters or space characters. %% Compare String Arrays for Equality % You can compare string arrays for equality with the relational operators |==| and % |~=|. When you compare string arrays, the output is a logical array that has |1| % where the relation is true, and |0| where it is not true. % % Compare two string scalars for equality. str1 = string('Hello'); str2 = string('World'); str1 == str2 %% % Compare a string array with multiple elements to a string scalar. C = {'Mercury','Gemini','Apollo';... 'Skylab','Skylab B','International Space Station'}; str1 = string(C); str2 = string('Apollo'); str1 == str2 %% % Compare a string array to a character vector. As long as one of the % variables is a string array, you can make the comparison. chr = 'Gemini'; TF = (str1 == chr) %% % Index into |str1| with |TF| to extract the string elements that matched % |Gemini|. You can use logical arrays to index into an array. str1(TF) %% % Compare for inequality using the |~=| operator. Index into |str1| to % extract the elements that do not match |'Gemini'|. TF = (str1 ~= chr) %% str1(TF) %% % Compare two nonscalar string arrays. When you compare two nonscalar % arrays, they must be the same size. str2 = string({'Mercury','Mars','Apollo';... 'Jupiter','Saturn','Neptune'}); TF = (str1 == str2) %% % Index into |str1| to extract the matches. str1(TF) %% Compare String Arrays with Other Relational Operators % You can also compare strings with the relational operators |>|, |>=|, % |<|, and |<=|. Strings that start with uppercase letters come before % strings that start with lowercase letters. For example, the string % |"ABC"| is less than |"abc"|. Digits and some punctuation marks % also come before letters. string('ABC') < string('abc') %% % Compare a string array that contains names to another name with the |>| % operator. The names |Sanchez|, |de Ponte|, and |Nash| come after % |Matthews|, because |S|, |d|, and |N| all are greater than |M|. str = string({'Sanchez','Jones','de Ponte','Crosby','Nash'}); TF = (str > 'Matthews') %% str(TF) %% Sort String Arrays % You can sort string arrays. MATLAB(R) stores characters as Unicode(R) using % the UTF-16 character encoding scheme. Character and string arrays are sorted % according to the UTF-16 code point order. For the characters that are also the % ASCII characters, this order means that uppercase letters come before lowercase % letters. Digits and some punctuation also come before letters. % % Sort the string array |str|. sort(str) %% % Sort a 2-by-3 string array. The |sort| function sorts the elements in % each column separately. sort(str2) %% % To sort the elements in each row, sort |str2| along the second dimension. sort(str2,2) %% Compare Character Vectors % You can compare character vectors and cell arrays of character vectors to % each other. Use the |strcmp| function to compare two character vectors, % or |strncmp| to compare the first |N| characters. You also can use % |strcmpi| and |strncmpi| for case-insensitive comparisons. % % Compare two character vectors with the |strcmp| function. |chr1| and % |chr2| are not equal. chr1 = 'hello'; chr2 = 'help'; TF = strcmp(chr1,chr2) %% % Note that the MATLAB |strcmp| differs from the C version of |strcmp|. The C version % of |strcmp| returns |0| when two character arrays are the same, not when % they are different. %% % Compare the first two characters with the |strncmp| function. |TF| is % |1| because both character vectors start with the characters |he|. TF = strncmp(chr1,chr2,2) %% % Compare two cell arrays of character vectors. |strcmp| returns a logical % array that is the same size as the cell arrays. C1 = {'pizza'; 'chips'; 'candy'}; C2 = {'pizza'; 'chocolate'; 'pretzels'}; strcmp(C1,C2) %% Inspect Characters in String and Character Arrays % You can inspect the characters in string arrays or character arrays % with the |isstrprop|, |isletter|, and |isspace| functions. % % * The |isstrprop| inspects characters in either string arrays or % character arrays. % % * The |isletter| and |isspace| functions inspect characters in % character arrays only. % % Determine which characters in a character vector are space characters. % |isspace| returns a logical vector that is the same size as |chr|. chr = 'Four score and seven years ago'; TF = isspace(chr) %% % The |isstrprop| function can query characters for many different % traits. |isstrprop| can determine whether characters in a string or % character vector are letters, alphanumeric characters, decimal or hexadecimal % digits, or punctuation characters. % % Determine which characters in a string are punctuation marks. |isstrprop| % returns a logical vector whose length is equal to the number of % characters in |str|. str = string('A horse! A horse! My kingdom for a horse!') %% isstrprop(str,'punct') %% % Determine which characters in the character vector |chr| are letters. isstrprop(chr,'alpha')