create a cell array of numbers

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 件のコメント

Jan
Jan 2013 年 6 月 16 日
Do you want a cell, which contains strings, or a matrix of type CHAR wither line breaks as separators?
Tobias Müller
Tobias Müller 2019 年 4 月 25 日
cell(linspace(1,100,1000))
;)
Walter Roberson
Walter Roberson 2019 年 4 月 25 日
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.

サインインしてコメントする。

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 16 日
編集済み: Azzi Abdelmalek 2013 年 6 月 16 日

0 投票

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

1 件のコメント

Walter Roberson
Walter Roberson 2020 年 5 月 28 日
This does not create a cell array.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 6 月 16 日

2 投票

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 件のコメント

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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by