필터 지우기
필터 지우기

fprintf cell array with strings and numbers

조회 수: 1 (최근 30일)
sal135
sal135 2017년 2월 3일
댓글: dpb 2017년 2월 7일
I have a matrix "C" 902x14cell and I need to write it using fprintf. Before the matrix I have 10 lines of header that contain only strings.
%%do this 10 times for each line of header
[rows10{kk},cols10{kk}]=size(header_row10{kk});
for i=1:rows10{kk}
fprintf(B{kk},'%s,',header_row10{kk}{i,1:end-1});
fprintf(B{kk},'%s\n',header_row10{kk}{i,end});
end
%Begin writing data after 10 lines of header
[rowsC{kk},cols1{kk}]=size(C{kk});
for i=1:rowsC{kk}
fprintf(B{kk},'%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,',C{kk}{i,1:end});
fprintf(B{kk},'%f%f%f%f%f%f%f%f%f%f%f%f%f%f\r\n',transpose(C{kk}{i,end}));
end
but when I do this it just creates a csv file of 1 row with all the columns mashed together. The only reason I need to use fprintf is because the matrix has 13 columns of number and 1 column of strings and dlmread doesn't support cells. Is there a way to fprintf C without it losing the columns?

답변 (1개)

dpb
dpb 2017년 2월 3일
...
fmt=[repmat('%f,',1,cols1{kk}-1) '%s\n']; % format presuming last is string
for i=1:rowsC{kk}
fprintf(B{kk},fmt,C{kk}{i,1:end-1},char(C{kk}(end)));
end
should do it altho a small sample of the actual data structure would be helpful.
  댓글 수: 2
sal135
sal135 2017년 2월 7일
The char column is the second column. So column 1 is made of doubles, column 2 is made of chars and columns 3-14 are made of doubles.
dpb
dpb 2017년 2월 7일
So, just rearrange the format string and output to match the layout...again the actual storage would be useful to get the indexing expressions right; the arrangement of {} looks peculiar in your code that I just mimicked.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by