how to export a 2 dimensional cell array of strings with different lengths to an excel file?
조회 수: 2 (최근 30일)
이전 댓글 표시
I understand XLSwrite only takes matrices. I cannot use matrices since every element is a string with different sizes
댓글 수: 0
채택된 답변
Walter Roberson
2016년 1월 9일
On MS Windows systems with Excel installed, xlswrite() is happy to take cell arrays. From the documentation:
"A: Input matrix, specified as a two-dimensional numeric or character array, or, if each cell contains a single element, a cell array.
If A is a cell array containing something other than a scalar numeric or a string, then xlswrite silently leaves the corresponding cell in the spreadsheet empty."
On MS Windows system that do not have Excel installed, and on OS-X and Linux systems, you are restricted to 'basic' mode, which does have a limitation of only supporting numeric data and csv output.
On those systems, if csv is acceptable, and for the case where A is a cell array containing only strings (and any empty entries are the empty string '' instead of the empty numeric []), then
fmt = [repmat('%s,', 1, size(A,2)), '%s\n'];
fid = fopen('YourOutput.csv', 'wt');
A_flip = A.'; %transpose needed
fprintf(fid, fmt, A_flip{:});
fclose(fid);
If you have a mix of strings and numeric values then more work is required to get the right output (especially if you have datenum or datetime values to be output as date strings.)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!