i have to write a cell which has both numbers and strings in to a text file if i use %s the numeric values are not written properly. Can any one help Thanks in advance

 채택된 답변

Wayne King
Wayne King 2012년 11월 6일

1 개 추천

You can use ischar() to query which elements are strings and write those properly.

댓글 수: 4

Walter Roberson
Walter Roberson 2012년 11월 6일
Expanding on this a bit: use (~cellfun(@ischar,YourArray)) to create a logical index of the numeric positions. Use cellfun() on those positions, with an appropriate function such as @sprintf, in order to format those numeric positions as strings and write them back in the cell array in the form of strings. Now you have a cell array which is only strings and you can write it out.
Sharen H
Sharen H 2012년 11월 6일
can u please give the syntax how to work i couldnot do it
Letting C be your cell array, then:
TC = C;
cellisnum = ~cellfun(@ischar, TC);
TC(cellisnum) = cellfun(@num2str, TC(cellisnum), 'UniformOutput', false);
for R = 1 : size(TC,1)
fprintf(s, '%s ', TC{R,1:end-1));
fprintf(s, '%s\n', TC{R,end});
end
Sharen H
Sharen H 2012년 11월 6일
Thanks a lot .. Walter Roberson ...Thank u so much

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2012년 11월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by