有没有一种简单的方法可以在 MATLAB 中从字符串创建首字母缩略词?例如:
'Superior Temporal Gyrus' => 'STG'
如果你想把每个大写字母都变成一个缩写...
...你可以使用函数 REGEXP :
str = 'Superior Temporal Gyrus'; %# Sample string
abbr = str(regexp(str,'[A-Z]')); %# Get all capital letters
... 或者您可以使用函数 UPPER和 ISSPACE :
abbr = str((str == upper(str)) & ~isspace(str)); %# Compare str to its uppercase
%# version and keep elements
%# that match, ignoring
%# whitespace
... 或者您可以改用 ASCII/UNICODE values对于大写字母:
abbr = str((str <= 90) & (str >= 65)); %# Get capital letters A (65) to Z (90)
如果你想将单词开头的每个字母都放入缩写中......
...你可以使用函数 REGEXP :
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
... 或者您可以使用 logical indexing 修改以上内容避免调用 FIND :
str = strtrim(str); %# Still have to trim whitespace
abbr = str([true isspace(str)]);
如果您想将单词开头的每个大写字母都放入缩写...
...你可以使用函数REGEXP :
abbr = str(regexp(str,'\<[A-Z]\w*'));
我是一名优秀的程序员,十分优秀!