Using num2str inside the for loop

조회 수: 3 (최근 30일)
Chiranjibi
Chiranjibi 2014년 11월 12일
답변: Star Strider 2014년 11월 12일
I'm trying to using num2str inside the for loop;
firstDay = 1;
lastDay = 5;
>> for i= firstDay:lastDay
f = num2str(i);
end
I was expecting f = 1 2 3 4 5, but I got f = 5 only. When I use f(i), this gives error though. Any help would be appreciated.

답변 (2개)

pietro
pietro 2014년 11월 12일
You got only 5 because you haven't used f as an array. Here the right code:
firstDay = 1;
lastDay = 5;
for i= firstDay:lastDay
f(i) = num2str(i);
end
  댓글 수: 1
Chiranjibi
Chiranjibi 2014년 11월 12일
Thanks, but if I use f(i) this also gives eror.

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


Star Strider
Star Strider 2014년 11월 12일
I don’t get an error subscripting it in R2014b, but there may be version differences.
A cell array should work:
firstDay = 1;
lastDay = 5;
for i= firstDay:lastDay
f{i} = num2str(i);
end
Note the curly braces ‘{}’ around the subscript, indicating a cell array.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by