Domanda

C'è un modo semplice per creare un acronimo da una stringa in MATLAB? Ad esempio:

'Superior Temporal Gyrus' => 'STG'
È stato utile?

Soluzione

Se si vuole mettere ogni lettera maiuscola in una sigla ...

... è possibile utilizzare la funzione REGEXP :

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

... o si potrebbe utilizzare le funzioni superiore e isspace :

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

... oppure si potrebbe invece fare uso dei href="http://www.cs.utk.edu/~pham/ascii_table.jpg" rel="nofollow noreferrer"> ASCII valori per le lettere maiuscole:

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

Se si vuole mettere ogni lettera che inizia una parola in un'abbreviazione ...

... è possibile utilizzare la funzione REGEXP :

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

... oppure si potrebbe utilizzare le funzioni STRTRIM , TROVA , e 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

... oppure si potrebbe modificare il sopra utilizzando logico indicizzazione per evitare la chiamata a TROVA :

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

Se si vuole mettere ogni capitale lettera che inizia una parola in un'abbreviazione ...

... è possibile utilizzare la funzione di REGEXP :

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

Altri suggerimenti

grazie, anche in questo modo:

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

restituirà sigla composta da lettere maiuscole nella stringa. Si noti la stringa deve essere in Sentence Caso

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top