将Excel(.xlsx)中的数据组合并读取到Matlab中

时间:2017-01-12 09:17:42

标签: excel matlab

我的查询分为两部分:

1)我有多个.xlsx文件存储在一个文件夹中,总共1年的价值(~365 .xlsx文件)。它们根据日期命名:' A_ddmmmyyyy.xlsx '(例如A_01Jan2016.xlsx)。每个.xlsx都有 5列数据日期,数量,纬度,经度,度量。问题是,每个.xlsx文件包含大约400,000行数据,虽然我在Excel中有脚本来合并它们,但Excel中固有的行限制阻止我将所有数据合并在一起。

(i)有没有办法递归地读取每个.xlsx工作表中的数据到MATLAB中,并在MATLAB中指定每列(变量)的变量名称(即日期,数量等)(没有列标题) .xlsx文件)?

(ii)如何将每个.xlsx中每列的数据合并在一起?

谢谢 杰弗逊

1 个答案:

答案 0 :(得分:0)

让我们按部分来看

首先我不建议将所有文件数据加入一列,不需要将这些信息全部放在一起,您可以单独使用它,例如使用数据存储区

在mya目录中的matlab中工作:

>> pwd

ans =

/home/anquegi/learn/matlab/stackoverflow

我有一个文件夹,其中包含两个示例excel文件:

>> ls 
20_hz.jpg  big_data_store_analysis.m  excel_files  octave-workspace  sample-file.log
40_hz.jpg  chirp_signals.m        NewCode.m    sample.csv

>> ls excel_files/
A_01Jan2016.xlsx  A_02Jan2016.xlsx

每个文件的内容是:

Date    Quantity    Latitude    Longitude   Measurement 
1   1   1   1   1 
2   2   2   2   2 
3   3   3   3   3 
4   4   4   4   4 
5   5   5   5   5 
6   6   6   6   6 
7   7   7   7   7 
8   8   8   8   8 
9   9   9   9   9
10  10  10  10  10 
11  11  11  11  11 
12  12  12  12  12 
13  13  13  13  13
14  14  14  14  14 
15  15  15  15  15 
16  16  16  16  16 
17  17  17  17  17
18  18  18  18  18 
19  19  19  19  19 
20  20  20  20  20 
21  21  21  21  21
22  22  22  22  22

只知道它将如何运作。

阅读数据:

>> ssds = spreadsheetDatastore('./excel_files')

ssds = 

  SpreadsheetDatastore with properties:

                      Files: {
                             '/home/anquegi/learn/matlab/stackoverflow/excel_files/A_01Jan2016.xlsx';
                             '/home/anquegi/learn/matlab/stackoverflow/excel_files/A_02Jan2016.xlsx'
                             }
                     Sheets: ''
                      Range: ''

  Sheet Format Properties:
             NumHeaderLines: 0
          ReadVariableNames: true
              VariableNames: {'Date', 'Quantity', 'Latitude' ... and 2 more}
              VariableTypes: {'double', 'double', 'double' ... and 2 more}

  Properties that control the table returned by preview, read, readall:
      SelectedVariableNames: {'Date', 'Quantity', 'Latitude' ... and 2 more}
      SelectedVariableTypes: {'double', 'double', 'double' ... and 2 more}
                   ReadSize: 'file'

现在,您拥有表中的所有数据,让我们看到预览

>> data = preview(ssds)

data = 

    Date    Quantity    Latitude    Longitude    Measurement
    ____    ________    ________    _________    ___________

    1       1           1           1            1          
    2       2           2           2            2          
    3       3           3           3            3          
    4       4           4           4            4          
    5       5           5           5            5          
    6       6           6           6            6          
    7       7           7           7            7          
    8       8           8           8            8    

预览是获取样本数据的好点。

您不需要合并,您可以完成所有元素:

>> ssds.VariableNames

ans = 

    'Date'    'Quantity'    'Latitude'    'Longitude'    'Measurement'

>> ssds.VariableTypes

ans = 

    'double'    'double'    'double'    'double'    'double'

% let's get all the Latitude elements that have Date equal 1, in this case the tow files are the same, so we wil get two elements with value 1

    >> reset(ssds)
    accum = [];
    while hasdata(ssds)
        T = read(ssds);
        accum(end +1) = T(T.Date == 1,:).Latitude;
    end
    >> accum

    accum =

         1     1

因此,您需要使用数据存储区和表,有点棘手但非常有用,您还希望控制数据存储区对象中的读取和其他变量。但这是在matlab中处理大数据文件的好方法

对于旧版本的matlab,您可以使用更传统的近似值:

folder='./excel_files';
filetype='*.xlsx';
f=fullfile(folder,filetype);
d=dir(f);
for k=1:numel(d);
  data{k}=xlsread(fullfile(folder,d(k).name));
end

现在您将数据存储在数据

folder='./excel_files';
filetype='*.xlsx';
f=fullfile(folder,filetype);
d=dir(f);
for k=1:numel(d);
data{k}=xlsread(fullfile(folder,d(k).name));
end
data

data =

[22x5 double]    [22x5 double]
  
    

数据{1}

  

ans =

 1     1     1     1     1
 2     2     2     2     2
 3     3     3     3     3
 4     4     4     4     4
 5     5     5     5     5
 6     6     6     6     6
 7     7     7     7     7
 8     8     8     8     8
 9     9     9     9     9
10    10    10    10    10
11    11    11    11    11
12    12    12    12    12
13    13    13    13    13
14    14    14    14    14
15    15    15    15    15
16    16    16    16    16
17    17    17    17    17
18    18    18    18    18
19    19    19    19    19
20    20    20    20    20
21    21    21    21    21
22    22    22    22    22

但要注意很多大文件

相关问题