String array of the letters of the alphabet

38 ビュー (過去 30 日間)
the cyclist
the cyclist 2023 年 2 月 10 日
コメント済み: the cyclist 2023 年 2 月 10 日
Is there a compact way to generate a string array with the letters of the English alphabet?
alphabet = ["a","b", .., "z"]

採用された回答

Voss
Voss 2023 年 2 月 10 日
編集済み: Voss 2023 年 2 月 10 日
string(('a':'z').').'
ans = 1×26 string array
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
  2 件のコメント
John D'Errico
John D'Errico 2023 年 2 月 10 日
Cute.
the cyclist
the cyclist 2023 年 2 月 10 日
Nice!

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 2 月 10 日
alf = 'a':'z'
alf = 'abcdefghijklmnopqrstuvwxyz'
It also works for caps.
ALF = 'A':'Z'
ALF = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  2 件のコメント
the cyclist
the cyclist 2023 年 2 月 10 日
Thanks, but two issues:
  • I want a string array, not a character array
  • I want 26 elements, each with one letter (not a single string with all letters)
John D'Errico
John D'Errico 2023 年 2 月 10 日
The obvious is:
alf = string('a':'z')
alf = "abcdefghijklmnopqrstuvwxyz"
That makes a string array, but you wanted a set of scalar string arrays. So then I see that this will create the desired string array.
string({'a' 'b'})
ans = 1×2 string array
"a" "b"
And that means you can use mat2cell.
string(mat2cell('a':'z',1,ones(1,26)))
ans = 1×26 string array
"a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
It feels a little kludgy. But it works. There may be a better way. If so, I'll probably think of it as soon as I click on submit.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by