How to create a cell of strings from a vector?

I have the vector
x = [ 10.027 20.35 50.1 100.002 ]
which I would like to convert to the cell
x = { '10', '20', '50', '100' }.
How can I do this? I know I can use sprintf('%.0f\n', x) to get the formatting I want, but not how to turn the outputs into a cell of strings.

 채택된 답변

Oleg Komarov
Oleg Komarov 2012년 9월 10일
편집: Oleg Komarov 2012년 9월 10일

0 개 추천

Encapsulate sprintf() into arrayfun():
arrayfun(@(in) sprintf('%.0f', in), x,'un',0)

댓글 수: 4

K E
K E 2012년 9월 10일
Thanks for the speedy and correct answer.
If you don't mind leading spaces:
cellstr(num2str(x','%.0f'))
  • pluses: no arrayfun
  • deltas: leading spaces
K E
K E 2012년 9월 10일
Very useful, thanks. I am more likely to recall cellstr next time.
If you add left justify to the format it removes the leading spaces:
cellstr(num2str(x','%-0.0f'))

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

추가 답변 (2개)

Jan
Jan 2012년 9월 10일
편집: Jan 2012년 9월 10일

1 개 추천

Perhaps this is faster:
Str = sprintf('%.0f*', x);
C = regexp(Str, '*', 'split');
Tom
Tom 2012년 9월 10일

1 개 추천

Based on Sean De Wolski's answer:
cellstr(num2str(x','%-0.0f'))

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

K E
2012년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by