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?

 채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 9일

2 개 추천

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
BN 2020년 4월 9일
편집: BN 2020년 4월 9일

1 개 추천

Try this:
writecell(X, 'X.xlsx')

댓글 수: 2

Walter Roberson
Walter Roberson 2020년 4월 9일
The result of that would be a single row with numel(a)+numel(b)+numel(c ) columns.
parham kianian
parham kianian 2020년 4월 10일
It does not work. Walter is right.

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

Qiang Lei
Qiang Lei 2022년 5월 20일

0 개 추천

writetable(cell2table(X), 'X.xlsx')

카테고리

질문:

2020년 4월 9일

답변:

2022년 5월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by