Filter löschen
Filter löschen

how to convert Cell to string

2 Ansichten (letzte 30 Tage)
Sharen H
Sharen H am 6 Nov. 2012
Suppose I have a cell
v = 'v' [576.5818] [3.0286] [576.9270]
'v' [576.5953] [3.1180] [576.8716]
'f' [ 56] [ 58] [ 52]
'f' [ 56] [ 58] [ 52]
and I want to convert this to a cell array using a format string for each
element:' %.5f' How can I do this? I tried the following approach, but I get an error:
f1 = @(x) sprintf(' %.5f',x);
cellfun(f1, num2cell(v),'UniformOutput', false)
i am getting an error as ???
Error using ==> sprintf
Function is not defined for 'cell' inputs.
Error in ==> @(x)sprintf(' %.5f',x)
Can any one help me thanks in advance

Akzeptierte Antwort

Kye Taylor
Kye Taylor am 6 Nov. 2012
Modify the command
cellfun(f1, num2cell(v),'UniformOutput', false)
to
cellfun(f1, v,'UniformOutput', false)
  3 Kommentare
Kye Taylor
Kye Taylor am 6 Nov. 2012
Create a function file such as
function write2file(x)
if ischar(x)
sprintf(' %s',x)
else
sprintf(' %.5f',x)
end
Then try
cellfun(@write2file,v,'UniformOutput',false)
Matt Fig
Matt Fig am 6 Nov. 2012
Bearbeitet: Matt Fig am 6 Nov. 2012
That is what num2str does automatically...
cellfun(@(x) num2str(x,'%.5f'),v,'un',0)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by