How to write elements of a nested cell array to an excel?

41 ビュー (過去 30 日間)
maruljay
maruljay 2019 年 7 月 29 日
編集済み: Andrei Bobrov 2019 年 7 月 29 日
Hi all,
I have nested cell array where each cell array contains a time series.
How do I write the contents of each of the cell array into rows in excel in the same sheet?
Like for example i want WS {2,1} to be written to the 2nd row in excel in the same sheet.
Capture.PNG

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 7 月 29 日
編集済み: Andrei Bobrov 2019 年 7 月 29 日
for old version of MATLAB
n = cellfun(@numel,WC);
k = cumsum(n);
ii = k - n + 1;
v = ones(k(end),1);
v(ii(2:end)) = v(ii(2:end)) - n(1:end-1);
A = accumarray([repelem((1:numel(n))',n),cumsum(v)],[C{:}]',[],[],nan);
xlswrite('yourxlsxfile.xlsx',A);

その他の回答 (2 件)

dpb
dpb 2019 年 7 月 29 日
Since all aren't same size, the trivial solution of
xlswrite(cell2mat(WS))
is out unless you augment the shorter to the length of longest.
If doing that is out, you'll have to either loop or use cellfun to write each array element.

TADA
TADA 2019 年 7 月 29 日
編集済み: TADA 2019 年 7 月 29 日
The problem starts with the different sizes of each rpw which makes it more complecated to unravel the nested cell arrays
c = {{1 2 3}; {1 2 3 4}; {1 2 3}};
% check row sizes
sizes = cellfun('length', c);
maxlen = max(sizes);
% equalize row sizes
for i = 1:numel(c)
c{i} = [c{i} cell(1,maxlen-sizes(i))];
end
% unravel cell array into a 2d cell array
c1 = vertcat(c{:});
% now you can export it easily
writecell(c1, 'c.xlsx', 'sheet', 1)

カテゴリ

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