How to create a cell of strings from a vector?

2 visualizaciones (últimos 30 días)
K E
K E el 10 de Sept. de 2012
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.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 10 de Sept. de 2012
Editada: Oleg Komarov el 10 de Sept. de 2012
Encapsulate sprintf() into arrayfun():
arrayfun(@(in) sprintf('%.0f', in), x,'un',0)
  4 comentarios
K E
K E el 10 de Sept. de 2012
Very useful, thanks. I am more likely to recall cellstr next time.
Tom
Tom el 10 de Sept. de 2012
If you add left justify to the format it removes the leading spaces:
cellstr(num2str(x','%-0.0f'))

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 10 de Sept. de 2012
Editada: Jan el 10 de Sept. de 2012
Perhaps this is faster:
Str = sprintf('%.0f*', x);
C = regexp(Str, '*', 'split');

Tom
Tom el 10 de Sept. de 2012
Based on Sean De Wolski's answer:
cellstr(num2str(x','%-0.0f'))

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by