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

조회 수: 17 (최근 30일)
芊帆 张
芊帆 张 2022년 4월 27일
답변: Lokesh 2023년 9월 29일
详见问题
  댓글 수: 1
CXD
CXD 2022년 4월 28일
x = ‘ABC’;
y = x-'A'+'1'

댓글을 달려면 로그인하십시오.

답변 (1개)

Lokesh
Lokesh 2023년 9월 29일
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

카테고리

Help CenterFile Exchange에서 字符和字符串에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!