Find ascii numbers from cells

9 visualizaciones (últimos 30 días)
Ivan Mich
Ivan Mich el 23 de En. de 2021
Comentada: Ivan Mich el 24 de En. de 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?

Respuesta aceptada

Adam Danz
Adam Danz el 23 de En. de 2021
Editada: Adam Danz el 23 de En. de 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 comentarios
Adam Danz
Adam Danz el 23 de En. de 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 el 24 de En. de 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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by