How to write cell array to excel file
25 次查看(过去 30 天)
显示 更早的评论
I have a cell array whose elements have different sizes. How can I write it to an excel file?
For example:
a = rand(10,1);
b = rand(5,1);
c = rand(4,8);
X = {a, b, c};
How can I export X to an excel file?
0 个评论
采纳的回答
Walter Roberson
2020-4-9
If the desired output is a worksheet in which the first column has 10 rows, and the second column has 5 rows, and then the next 8 columns have 4 rows, with the unused portions of each column being empty: then you cannot directly do that.
However, you can use:
vars = {a,b,c};
varnames = {'a', 'b', 'c'};
rows = cellfun(@(M) size(M,1), vars);
cols = cellfun(@(M) size(M,2), vars);
maxrows = max(rows);
T = table();
for K = 1 : length(varnames)
T.(varnames{K}) = [vars{K}; nan(maxrows - rows(K), cols(K))];
end
writetable(T, 'X.xlsx');
更多回答(2 个)
BN
2020-4-9
编辑:BN
2020-4-9
Try this:
writecell(X, 'X.xlsx')
2 个评论
Walter Roberson
2020-4-9
The result of that would be a single row with numel(a)+numel(b)+numel(c ) columns.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!