"randi(100)" Outputting Characters Instead of Integers

In line 2 below, "randi(100)" is returning characters, including black spaces, instead of integers. What code is converting "randi(100)" to return characters above line XXX, and how can I change this code to return integers?
dstr=num2str(d);
name=strcat('d',dstr,'_',randi(100));

댓글 수: 4

per isakson
per isakson 2015년 10월 18일
편집: per isakson 2015년 10월 18일
That's the way strcat works ( str as in string)
>> strcat( 97:107 )
ans =
abcdefghijk
Is this what you want?
>> dstr = 'hello';
>> name=strcat('d',dstr,'_',num2str( randi(100) ));
>> name
name =
dhello_82
balsip
balsip 2015년 10월 18일
Thanks to you as well, Per Isakson. Didn't realize that tacking on another "num2str" would work.
IMO: it's better to use sprintf
>> name = sprintf( 'd%s_%d', dstr, randi(100) )
name =
dhello_91
balsip
balsip 2015년 10월 18일
That would work, too! Thanks again.

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

 채택된 답변

the cyclist
the cyclist 2015년 10월 18일
편집: the cyclist 2015년 10월 18일
I think you might want
name=strcat('d',dstr,'_',num2str(randi(100)));
This will convert the number to its string equivalent, rather than the ASCII value corresponding to that value (which is what I assume is happening now).

댓글 수: 1

balsip
balsip 2015년 10월 18일
Thanks for the quick reply, Cyclist. That did the trick. Very green here, so it's much appreciated.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

질문:

2015년 10월 18일

댓글:

2015년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by