Find ascii numbers from cells

4 次查看(过去 30 天)
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?

采纳的回答

Adam Danz
Adam Danz 2021-1-23
编辑:Adam Danz 2021-1-23
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 ! '
  3 个评论
Adam Danz
Adam Danz 2021-1-23
How are you reading in the file
C = readcell('names.xlsx');
a = cellfun(@double, C, 'UniformOutput', false)
% a =
% 7×1 cell array
% {1×4 double}
% {1×6 double}
% {1×6 double}
% {1×4 double}
% {1×4 double}
% {1×3 double}
% {1×7 double}
Ivan Mich
Ivan Mich 2021-1-24
Thank you. I have one more question. I would like to use crosscorr funtion, in order to correlate each cell (to correlate cell 1st column with the cell in second column etc).
my code is
Pin = readcell('names_2.xlsx');
A = cellfun(@double, Pin, 'UniformOutput', false);
a=A(2:end,1);
b=A(2:end,2);
[c,lag]=crosscorr(a,b)
But command window shows me :
The value of 'y1' is invalid. Expected first input series to be one of these types:
double
Instead its type was cell.
Could you help me please?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by