2017-04-24 93 views
0

我想导出1xm-Cellarray使用xlswrite。单元阵列由m小区,每个小区包含ixn-Cellarray其中i主要是2,但也可以是3,4,5或6。这里的数据是什么样子的例子:我想所有将数据导出到Excel并对其应用格式

a=[{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}},{{{'a'},{'b'},{'c'},{'d'};{'a'},{'b'},{'c'},{'d'}}}] 

a = 

    {2x4 cell} {3x4 cell} {2x4 cell} 

这些单元格要在下面写下来,但我希望能够在Excel中看到哪些行属于一个单元格。我的想法是把一个空行之间的阵列细胞和另一种这样

exportTable=[]; 
for jj=1:numel(a) 
    exportTable=[exportTable;a{jj};repmat({[]},1,18)]; 
end 

,然后使用xlswrite导出exportTable,但是这看起来在导出表好看不好看,不容易阅读。
现在我正在寻找一种方法,可以直接使用matlab中的导出函数或者使用Excel将相应行的矢量作为输入来获取同一颜色中各个单元格的颜色线。
我可以使用

rows=cumsum(cellfun(@(x) size(x,1),a)) 

rows = 

    2  5  7 

实现每个单元的结束索引,但我不知道如何着色基于rownumbers Excel中的行。
在我的例子所需要的输出应该是这样的:
example output

利用Matlab或Excel任何帮助表示赞赏。

+0

我会在Excel中制作一个简单的宏。我认为在matlab中格式化会变得非常难看。 –

+0

@ValerioRaco那么,你如何在Excel中创建这样一个宏? – Max

回答

1
rows = cumsum(cellfun(@(x) size(x,1),a)) 

%Create an Excel object. 
e = actxserver('Excel.Application'); 

%Add a workbook. 
eWorkbook = e.Workbooks.Add; 
e.Visible = 1; 

%Make the first sheet active. 
eSheets = e.ActiveWorkbook.Sheets; 
eSheet1 = eSheets.get('Item',1); 
eSheet1.Activate 

for i = 1:length(a) 
    ai = table2array(cell2table(a{:,i})); % sorry for this construction 

    if mod(i,2) 
     ai_color = 3; 
    else 
     ai_color = 4; 
    end 

    ai_range = ['A',num2str(rows(i)-size(ai,1)+1),':',char('A'-1+size(ai,2)),num2str(rows(i))]; % ... and this :) 

    % Set the color of cells 
    eSheet1.Range(ai_range).Interior.ColorIndex = ai_color; 

    %Put MATLAB data into the worksheet. 
    eActivesheetRange = get(e.Activesheet,'Range',ai_range); 
    eActivesheetRange.Value = ai; 
end 


SaveAs(eWorkbook,'myfile.xlsx') 

%If the Excel program displays a dialog box about saving the file, select the appropriate response to continue. 

%If you saved the file, then close the workbook. 
eWorkbook.Saved = 1; 
Close(eWorkbook) 

%Quit the Excel program and delete the server object. 
Quit(e) 
delete(e) 

%Note: Make sure that you close workbook objects you create to prevent potential memory leaks. 
+0

看起来很稳固。不幸的是我现在无法尝试,因为我家里没有windows系统。我会在一周内尝试,并给你反馈/接受你的答案。尽管非常感谢你的努力! :) – Max

+0

完全解决了我的问题,非常感谢你再次! – Max