MATLAB: How to generate a vector of character values

changing number in a loop

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?

Best Answer

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?