Find ascii numbers from cells

3 views (last 30 days)
Ivan Mich
Ivan Mich on 23 Jan 2021
Commented: Ivan Mich on 24 Jan 2021
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?

Accepted Answer

Adam Danz
Adam Danz on 23 Jan 2021
Edited: Adam Danz on 23 Jan 2021
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 Comments
Adam Danz
Adam Danz on 23 Jan 2021
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 on 24 Jan 2021
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!