MATLAB export cell array to csv file
이전 댓글 표시
Hi all,
I wish to write a some information in form of a csv file from a matlab code.
In the MATLAB code I have stored the header as a cell array:
ToCSV={'Location' 'Weight_factor' 'Average' 'Maximum' 'Minimum'};
I append rows to this cell array by looping. A sample statement is:
ToCSV={ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} };
I wish to print this as a csv file. I used following routine which sort of giving errors:
fid = fopen('outfile.csv','w');
fprintf(fid,'%s, %s, %s, %s, %s\n',ToCSV{1,:});
fprintf(fid,'%s, %f, %10.2e, %10.2e, %f\n',ToCSV{2:end,:});
fclose(fid);
>>Error using fprintf
>>Function is not defined for 'cell' inputs.
Can some of you please provide pointers to achieve this ? I also tried csvwrite but apparently it doesn't go well with cell arrays.
Thanks a lot !!!
채택된 답변
추가 답변 (1개)
Walter Roberson
2013년 12월 5일
Change
ToCSV={ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} };
to
ToCSV=[ToCSV; {'EastLocation' 0.5 1e+3 1e+4 1e+2} ];
댓글 수: 3
Rahul Pandey
2013년 12월 5일
Walter Roberson
2013년 12월 5일
What do you want to appear in the first column in the csv file?
If you want the cell array of strings to appear, separated by spaces, then
str_to_store = sprintf('%s ', ToCSV{row,1});
You can automate this with something like
FirstCol = cellfun( @(C) sprintf('%s ', C{:}), ToCSV(:,1), 'Uniform', 0);
Rahul Pandey
2013년 12월 5일
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!