How to write cell array to excel file

44 ビュー (過去 30 日間)
parham kianian
parham kianian 2020 年 4 月 9 日
回答済み: Qiang Lei 2022 年 5 月 20 日
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 日
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 日
+1
parham kianian
parham kianian 2020 年 4 月 10 日
It works well.

サインインしてコメントする。

その他の回答 (2 件)

BN
BN 2020 年 4 月 9 日
編集済み: BN 2020 年 4 月 9 日
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 日
writetable(cell2table(X), 'X.xlsx')

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by