how to convert Cell to string

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

 채택된 답변

Kye Taylor
Kye Taylor 2012년 11월 6일

1 개 추천

Modify the command
cellfun(f1, num2cell(v),'UniformOutput', false)
to
cellfun(f1, v,'UniformOutput', false)

댓글 수: 3

Sharen H
Sharen H 2012년 11월 6일
편집: Sharen H 2012년 11월 6일
perfectly it works but the first column value is changed to number instead of 'v'
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 2012년 11월 6일
편집: Matt Fig 2012년 11월 6일
That is what num2str does automatically...
cellfun(@(x) num2str(x,'%.5f'),v,'un',0)

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

추가 답변 (0개)

카테고리

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

질문:

2012년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by