将MATLAB数据提取到一个Excel电子表格中

将MATLAB数据提取到一个Excel电子表格中,excel,matlab,Excel,Matlab,首先是一个MATLAB代码(伪代码): 每个循环中A和B的值如下所示 A(1) = 'Rank' 'Ticker' '201205' 'Weight' [ 1] 'B' [ 0.04] [ 0.03] [ 2] 'E' [ 0.01] [ 0.10] A(2) = 'Rank' 'Ticker' '201206' 'Weight'

首先是一个MATLAB代码(伪代码):

每个循环中A和B的值如下所示

A(1) =     
'Rank'    'Ticker'    '201205'    'Weight'
[   1]    'B'         [ 0.04]     [       0.03]
[   2]    'E'         [ 0.01]     [       0.10]


A(2) =     
    'Rank'    'Ticker'    '201206'    'Weight'
    [   1]    'X'         [ 0.02]     [       0.07]
    [   2]    'Y'         [ 0.01]     [       0.11]


B(1) =     
'Rank'    'Sector'    '201205'    
[   1]    'S'         [ 0.02]    
[   2]    'A'         [ 0.01]   


B(2) =     
    'Rank'    'Sector'    '201206'  
    [   1]    'T'         [ 0.08]   
    [   2]    'Y'         [ 0.03]    
现在来问我的问题。如何在循环中添加代码,以便从每个循环中生成的a和B的所有值都可以提取到单个电子表格中,如链接中显示的下图所示

诀窍在于数据应该在电子表格的不同区域为不同的i提取


顺便说一句,这是一个简化版本。实际问题有500个循环,而不是2个。A和B也要复杂得多。

您可以将其全部转储到CSV文件(逗号分隔的值)

只需在for循环中添加一个累积所有数据的文本变量,它将如下所示:

table = {};
res_wid = 2;
res_height = 3;
dist = 2;
for i=1:10
    x_pos = (i-1)*(res_wid + dist)+ (1:res_wid);
    for j=1:10
        y_pos = (j-1)*(res_height + dist)+ (1:res_height);
        data = {
            'label 1','label 2'
            rand, rand
            rand, rand
            };
        table(y_pos,x_pos) = data;
    end
end
(somecsvfile.csv的内容)


我通常是这样做的:

table = {};
res_wid = 2;
res_height = 3;
dist = 2;
for i=1:10
    x_pos = (i-1)*(res_wid + dist)+ (1:res_wid);
    for j=1:10
        y_pos = (j-1)*(res_height + dist)+ (1:res_height);
        data = {
            'label 1','label 2'
            rand, rand
            rand, rand
            };
        table(y_pos,x_pos) = data;
    end
end

请注意,在编写这些文件时,excel有一些愚蠢的限制:65536行乘256列(请参阅)。您可以轻松地在这些环境中运行。

感谢您的及时回复和有关Excel限制的警告。然而,我想知道这段代码如何将MATLAB矩阵中的数据提取到电子表格中?请参考这些线程,
table = {};
res_wid = 2;
res_height = 3;
dist = 2;
for i=1:10
    x_pos = (i-1)*(res_wid + dist)+ (1:res_wid);
    for j=1:10
        y_pos = (j-1)*(res_height + dist)+ (1:res_height);
        data = {
            'label 1','label 2'
            rand, rand
            rand, rand
            };
        table(y_pos,x_pos) = data;
    end
end