How to add one digit to another digits?

조회 수: 1 (최근 30일)
Eggo Ogge
Eggo Ogge 2011년 3월 1일
For example, I have a number b=5; and i need to add it by another digits, for example 1234 and get the answer 12345. Code may show like: set(handles.text1,'string',1234+b) An error is "1234+b" and i don't know how format it that one digit adds to another digits. Thanks in advance.

채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 1일
strcat('1234','5')
you can convert any number to string with num2str or other similar function
strcat('1234',num2str(5))
b=5;
strcat('1234',num2str(b))
  댓글 수: 1
Eggo Ogge
Eggo Ogge 2011년 3월 1일
Thank you, i understand this.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 3월 1일
The answer to your question as phrased is:
set(handles.text1, 'string', 1234*10^max(1,ceil(log10(b+1))) + b)
I would suggest, though, that you instead consider
set(handles.text1, 'string', sprintf('%d%d', 1234, b))
EDIT: put in max() to correct for the possibility that b is 0.
  댓글 수: 4
Eggo Ogge
Eggo Ogge 2011년 3월 2일
Can I ask one more question? If there is not digits, but string, i have something like '=a21:' and by this add hexadecimal number.
g=2F4
sprintf('%s%x',=a21:,g). Can it be something like that?
Walter Roberson
Walter Roberson 2011년 3월 2일
sprintf('%s%s', '=a21:', '2F4');
Or more simply
['=a21:', '2F4']
It is not possible to create a _numeric_ value in hex format.
Perhaps, though, you might like to use
sprintf('%s%x', '=a21:', hex2dec('2F4'))

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by