How to convert and display numbers into strings

조회 수: 13 (최근 30일)
Emerson De Souza
Emerson De Souza 2013년 7월 23일
INTRODUCTION: Hi, I make a string concatenation using alpha and numerical characters
['K=' num2str(k)]
Thus, using the loop below
for k=998:1001
['K=' num2str(k)]
end;
I obtain the following strings K=998, K=999, K=1000, K=1001
which I use elsewhere.
PROBLEM/WISH: I would like TO ADD ONE ZERO BEFORE the number as below:
K=0998, K=0999, K=1000, K=1001
and I would like TO ADD TWO ZEROS if k<100, and THREE ZEROS if k<10
I wonder if someone could help to fix this problem.
Thank you Emerson

채택된 답변

David Sanchez
David Sanchez 2013년 7월 23일
for k=1:1000
if k<10
tmp_str = 'K=000';
elseif k<100
tmp_str = 'K=00';
elseif k>=100 && k<1000
tmp_str = 'K=0';
else
tmp_str = 'K=';
end
[tmp_str num2str(k)]
end
  댓글 수: 1
Emerson De Souza
Emerson De Souza 2013년 7월 23일
Thank you David, your suggestion fixed the problem.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by