Using num2str inside the for loop

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.

Respuestas (2)

pietro
pietro el 12 de Nov. de 2014
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 comentario

Chiranjibi
Chiranjibi el 12 de Nov. de 2014
Thanks, but if I use f(i) this also gives eror.

Iniciar sesión para comentar.

Star Strider
Star Strider el 12 de Nov. de 2014
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.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 12 de Nov. de 2014

Respondida:

el 12 de Nov. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by