Is there a way to get the num2str without spaces?

54 ビュー (過去 30 日間)
Akshar Agarwal
Akshar Agarwal 2017 年 10 月 12 日
コメント済み: Akshar Agarwal 2017 年 10 月 12 日
I have a matrix, a=[1:100]. I want to get a matrix c, with the digits of 1:100. To do so, I first did
b=num2str(a),
which gets me the string version but it has spaces. If it did not have spaces, I could make a loop as follows:
for i=1:size(b,2)
c(i)=b(i);
end
Since the num2str(a) has spaces, I cannot just select the ith element of b and get something meaningful.

採用された回答

Image Analyst
Image Analyst 2017 年 10 月 12 日
Try sprintf():
b = sprintf('%d', a); % If a is an integer.
b = sprintf('%f', a); % If a is a double.
  2 件のコメント
Image Analyst
Image Analyst 2017 年 10 月 12 日
Or
c = sprintf('%d, ', b); % b is an array.
which will put a comma and a space between all the numbers and return a single string called c with all the numbers in it.
Akshar Agarwal
Akshar Agarwal 2017 年 10 月 12 日
Thanks, could you explain the %d and %f please?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by