【问题标题】:Combining and reading data from Excel (.xlsx) into Matlab将 Excel (.xlsx) 中的数据合并并读取到 Matlab 中
【发布时间】:2017-05-27 07:47:11
【问题描述】:

我的查询分为两部分:

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 中每一列的数据合并在一起?

谢谢 杰斐逊

【问题讨论】:

  • 如果 Excel 中的数组大小限制了您,那么您将无法将所有数据放在单个 xlsx 文件中,而不管 MATLAB 是什么。您当然可以将数据保存在 MATLAB 中。 “合并每一列的数据”是什么意思?总结他们?连接它们?
  • 尝试使用xlsread 读取文件。您可以使用dir 函数来获取所有文件名,如下所示:files = dir('myfolder/A_*.xlsx');。获得一些代码后,您可以在此处发布您遇到的任何具体问题。

标签: excel matlab


【解决方案1】:

让我们分部分进行

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

在 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

现在您已经将数据存储在 data 中了

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

数据 =

[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

但要小心很多大文件

【讨论】:

  • 奇怪,为什么会说:未定义的函数或变量'spreadsheetDatastore'。当我尝试运行该功能时?我在 2014a 上
  • 我更新了,使用旧的matlab版本,但效率不高
  • 大文件有办法吗?顺便问一下,电子表格数据存储是从哪个版本的matlab开始的?
  • 2016年推出es.mathworks.com/help/matlab/ref/…,可以试试xlsread by blocks
  • 感谢您的澄清,但块是什么意思?
猜你喜欢
  • 2017-07-11
  • 2017-02-24
  • 1970-01-01
  • 2021-06-25
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 2013-07-15
  • 1970-01-01
相关资源
最近更新 更多