3

MATLABで文字列から頭字語を作成する簡単な方法はありますか?例えば:

'Superior Temporal Gyrus' => 'STG'
4

2 に答える 2

8

すべての大文字を略語にしたい場合...

...関数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値を使用できます。

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

...または、関数STRTRIMFIND、および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*'));
于 2010-06-14T16:10:39.197 に答える
0

ありがとう、これも:

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

文字列内の大文字で構成される省略形を返します。文字列は文の場合でなければならないことに注意してください

于 2010-06-14T17:17:39.810 に答える