How to convert a numerical matrix into a matrix of formatted strings?
Mostrar comentarios más antiguos
Hi,
I'd like to convert a numerical matrix into a matrix of formatted strings.
Example of input matrix:[1 2 3.051 4]
What I expect as output: {'1.00' '2.00' '3.05' 4.00'} specified by the string format '%.2f' for instance.
Thanks for your tips.
[EDITED, JSimon, 06-Oct-2011 14:16 UTC]: Cell string wanted => square brackets to curly braces.
Respuestas (4)
Walter Roberson
el 6 de Oct. de 2011
cellstr(num2str(matrix(:),'%.2f')).'
Grzegorz Knor
el 6 de Oct. de 2011
use sprintf:
sprintf('%.2f ',[1 2 3.051 4])
Benjamin
el 6 de Oct. de 2011
2 comentarios
Grzegorz Knor
el 6 de Oct. de 2011
Shorter solution:
V=[1;2;3.051;4];
M=[V 2*V];
C = arrayfun(@(x)sprintf('%.2f',x),M,'Uniform',false)
Jan
el 6 de Oct. de 2011
Or as loop:
C = cell(size(M)); for i=1:numel(M); C{i}=sprintf('%.2f', M(i)); end
Using the linear index for M and C reduces the complexity.
Benjamin
el 12 de Oct. de 2011
0 votos
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!