How to generate a vector of character values?
7 views (last 30 days)
Show older comments
I want to generate a vector A with all character values 1, 2, 3...N; For example, A=['1','2','3'...], N is changeable and can be defined beforehand. How can I do that?
0 Comments
Accepted Answer
Guillaume
on 9 Oct 2018
Edited: Guillaume
on 9 Oct 2018
Your question is not very clear. Note that character '1' has a value of 49. Also note that there are only 10 characters which represents numerical digits, the characters '0123456789'. Finally also note that A=['1','2','3','4'] is exactly the same as A='1234'. If you want separate char arrays, each a textual representation of number you need to put the array in a cell array, A={'1','2','3','4'}, or a string array, A=["1","2","3","4"].
So, if you wanted to generate all characters with values between 1 and N, you'd use
char(1:N)
Be aware that characters with value below 32 are non-printable characters.
If you wanted to generate the digit characters starting at '1' up to maximum of 9, then
char((1:N) + '0')
Of course, if N is greater than 9, this will generate non-digit characters since as stated there's only 10 of them (and '0' is before '1')
If you wanted to generate a cell array of char array, each the numeric representation of an integer from 1 to N (with N potentially greater than 9):
compose('%d', 1:N) %for a cell array of char arrays
compose("%d", 1:N) %for a string array
Did I mention that the question was not clear?
More Answers (1)
Steven Lord
on 9 Oct 2018
Does it have to be a char vector, or is a string vector acceptable?
N = 42;
S = string(1:N)
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!