MATLAB: Find ascii numbers from cells

arrayasciicellcommandnumbers

I would like to find ascii numbers from cells. My file has words/letters (cell arrays). I would like to calculate ascii number for each cell.
Could you help me please?

Best Answer

Matlab stores characters as Unicode characters which incorporates the ASCII character set as the first 128 sumbols.
Convert characters -->Unicode/ASCII using double('__')
Convert Unicode/ASCII-->characters using char(__)
double('a')
ans = 97
double('Matlab')
ans = 1×6
77 97 116 108 97 98
char([77 97 116 108 97 98])
ans = 'Matlab'
double('?') % Not ASCII
ans = 1×2
55357 56846
To apply that to a cell array,
z = {'Here','is an','example','!',''};
a = cellfun(@double, z, 'UniformOutput', false)
a = 1x5 cell array
{1×4 double} {1×5 double} {1×7 double} {[33]} {0×0 double}
a{1} % "Here"
ans = 1×4
72 101 114 101
a{2} % "is an"
ans = 1×5
105 115 32 97 110
--or--
v = cell2mat(cellfun(@double, z, 'UniformOutput', false))
v = 1×17
72 101 114 101 105 115 32 97 110 101 120 97 109 112 108 101 33
Put the cell array of ASCII codes back into char vector
c = strjoin(cellfun(@char, a, 'UniformOutput', false))
c = 'Here is an example ! '