hi
i need to create a cell array with numbers from 0000-1000
notice that numbers must be in a 4-digit format
how can i do this?
thanks

3 Kommentare

Jan
Jan am 16 Jun. 2013
Do you want a cell, which contains strings, or a matrix of type CHAR wither line breaks as separators?
cell(linspace(1,100,1000))
;)
Walter Roberson
Walter Roberson am 25 Apr. 2019
linspace(1,100,1000) would contain non-integer values, which cell would not be able to use a dimension sizes to construct a 1000-dimensional cell array.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 16 Jun. 2013
Bearbeitet: Azzi Abdelmalek am 16 Jun. 2013

0 Stimmen

str = sprintf('%04d\n',0:1000)

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 16 Jun. 2013

2 Stimmen

No. In MATLAB, numbers automatically drop leading 0's when displayed.
What is possible is to create a cell array of strings from '0000' to '1000'.
cellstr(num2str(0:1000).', '%04d')
Note: the .' is required there; without it the strings would come out wrong.

5 Kommentare

noa
noa am 16 Jun. 2013
"??? Error using ==> cellstr
Too many input arguments."
cellstr(num2str(0:1000, '%04d').')
Xiangrui Li
Xiangrui Li am 28 Mai 2020
Bearbeitet: Xiangrui Li am 28 Mai 2020
It should be
cellstr(num2str((0:1000).', '%04d'))
Walter Roberson
Walter Roberson am 28 Mai 2020
Thank you, Xiangrui Li, you are correct.
There is also the undocumented
sprintfc('%04d', 0:1000)
Also since R2016b, there has also been
compose('%04d', 0:1000)

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by