如何将数字自动转化为​字母?比如输入ABC​,输出123

17 visualizaciones (últimos 30 días)
芊帆 张
芊帆 张 el 27 de Abr. de 2022
Respondida: Lokesh el 29 de Sept. de 2023
详见问题
  1 comentario
CXD
CXD el 28 de Abr. de 2022
x = ‘ABC’;
y = x-'A'+'1'

Iniciar sesión para comentar.

Respuestas (1)

Lokesh
Lokesh el 29 de Sept. de 2023
Hi,
I understand that you want to convert string to a number.
To convert letters to numbers in MATLAB, you can leverage the ASCII values of the characters. In ASCII, the characters 'A' to 'Z' have sequential decimal values from 65 to 90. By subtracting the ASCII value of 'A' from a letter's ASCII value, you can obtain its corresponding number.
Here's an example of how you can convert a string of letters to their corresponding numbers:
% Convert a string of letters to numbers
% The string of letters to convert
letters = 'ABC';
% Convert the letters to numbers [1,2,3]
numbers = double(letters) - 64;
% Convert the numbers to a string without spaces ‘123’
str = sprintf('%d', numbers)
You can refer to the below documentation to know more about usage of “sprintf”:
I hope you find this helpful.
Best Regards,
Lokesh

Categorías

Más información sobre 字符和字符串 en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!