Write different sized cell arrays of strings to one excel sheet

1 回表示 (過去 30 日間)
Marcus Glover
Marcus Glover 2020 年 9 月 11 日
コメント済み: Ameer Hamza 2020 年 9 月 11 日
I would like to list the string values contained in cell arrays of different sizes in their own column in excel. Here's my example and goal:
c1={'The' 'quick' 'brown'} %this should be the first column in excel
c2={'fox' 'jumps'} %this should be the second column in excel
Goal (these are rows and columns in excel):
The fox
quick jumps
brown
My idea was to run a big loop and write each cell array as a column piecemeal using the range option in xlswrite, but was hoping there was a faster way to make one large padded array in MATLAB or something first, as I have a lot of data to write.

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 11 日
Try this
c{1} = {'The' 'quick' 'brown'}; %this should be the first column in excel
c{2} = {'fox' 'jumps'}; %this should be the second column in excel
c{3} = {'over' 'the', 'lazy'};
c{4} = {'dog'};
max_size = max(cellfun(@numel, c));
c_padded = cellfun(@(x) [x.'; cell(max_size-numel(x), 1)], c, 'uni', 0);
c_padded = [c_padded{:}];
writecell(c_padded, 'filename.xlsx')
  2 件のコメント
Marcus Glover
Marcus Glover 2020 年 9 月 11 日
Perfect, Thanks!
I wish I learned about writecell years ago!
Ameer Hamza
Ameer Hamza 2020 年 9 月 11 日
I am glad to be of help!!
If by "years" you mean 1.5 years ;) because writecell was recently added to MATLAB in R2019a.

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

その他の回答 (0 件)

カテゴリ

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