How to create an abbreviation from a string in MATLAB?

Is there an easy way to create an abbreviation from a string in MATLAB? For instance:

'Superior Temporal Gyrus' => 'STG'
+3
source share
2 answers

If you want to put each capital letter in the abbreviation ...

... you can use the REGEXP function :

str = 'Superior Temporal Gyrus';  %# Sample string
abbr = str(regexp(str,'[A-Z]'));  %# Get all capital letters

... or you can use the UPPER and ISSPACE functions :

abbr = str((str == upper(str)) & ~isspace(str));  %# Compare str to its uppercase
                                                  %#   version and keep elements
                                                  %#   that match, ignoring
                                                  %#   whitespace

... or instead, you can use ASCII / UNICODE values for capital letters:

abbr = str((str <= 90) & (str >= 65));  %# Get capital letters A (65) to Z (90)


If you want to put every letter that starts the word in the abbreviation ...

... you can use the REGEXP function :

abbr = str(regexp(str,'\w+'));  %# Get the starting letter of each word

... STRTRIM, FIND ISSPACE:

str = strtrim(str);  %# Trim leading and trailing whitespace first
abbr = str([1 find(isspace(str))+1]);  %# Get the first element of str and every
                                       %#   element following whitespace

... , , FIND:

str = strtrim(str);  %# Still have to trim whitespace
abbr = str([true isspace(str)]);


, ...

... REGEXP:

abbr = str(regexp(str,'\<[A-Z]\w*'));
+8

, :

s1(regexp(s1, '[A-Z]', 'start'))

, . ,

0

Source: https://habr.com/ru/post/1749961/


All Articles