Main Content

数据存储快速入门

什么是数据存储?

数据存储是一个用于读取单个文件或者文件或数据集合的对象。它相当于一个存储库,用来存储具有相同结构和格式的数据。例如,数据存储中每个文件包含的数据必须具有相同的类型(如数字或文本)、以相同顺序显示并用相同的分隔符分隔。

Several spreadsheets with the same columns in the same order

在以下情况下,数据存储很有用:

  • 集合中的每个文件可能太大,无法放入内存中。数据存储允许您从每个文件可放入内存的较小部分中读取和分析数据。

  • 集合中的文件具有任意名称。数据存储充当一个或多个文件夹中文件的存储库。这些文件不需要具有序列名称。

可根据数据或应用程序的类型来创建数据存储。不同类型的数据存储包含与其支持的数据类型相关的属性。例如,请参阅下表了解 MATLAB® 数据存储列表。有关完整的数据存储列表,请参阅Select Datastore for File Format or Application

文件或数据的类型数据存储类型
包含列向数据的文本文件,包括 CSV 文件。TabularTextDatastore
图像文件,包括 imread 支持的格式,如 JPEG 和 PNG。ImageDatastore
具有支持的 Excel® 格式(如 .xlsx)的电子表格文件。SpreadsheetDatastore
作为 mapreduce 的输入或输出的键-值对组数据。KeyValueDatastore
包含列向数据的 Parquet 文件。ParquetDatastore
自定义文件格式。需要提供用于读取数据的函数。FileDatastore
用于存放 tall 数组的检查点的数据存储。TallDatastore

创建和读取数据存储

使用 tabularTextDatastore 函数从示例文件 airlinesmall.csv 创建一个数据存储,其中包含有关各航空公司航班的出发和到达信息。结果是一个 TabularTextDatastore 对象。

ds = tabularTextDatastore('airlinesmall.csv')
ds = 

  TabularTextDatastore with properties:

                      Files: {
                             ' ...\matlab\toolbox\matlab\demos\airlinesmall.csv'
                             }
                    Folders: {
                             ' ...\matlab\toolbox\matlab\demos'
                             }
               FileEncoding: 'UTF-8'
   AlternateFileSystemRoots: {}
      PreserveVariableNames: false
          ReadVariableNames: true
              VariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more}
             DatetimeLocale: en_US

  Text Format Properties:
             NumHeaderLines: 0
                  Delimiter: ','
               RowDelimiter: '\r\n'
             TreatAsMissing: ''
               MissingValue: NaN

  Advanced Text Format Properties:
            TextscanFormats: {'%f', '%f', '%f' ... and 26 more}
                   TextType: 'char'
         ExponentCharacters: 'eEdD'
               CommentStyle: ''
                 Whitespace: ' \b\t'
    MultipleDelimitersAsOne: false

  Properties that control the table returned by preview, read, readall:
      SelectedVariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more}
            SelectedFormats: {'%f', '%f', '%f' ... and 26 more}
                   ReadSize: 20000 rows
                 OutputType: 'table'
                   RowTimes: []

  Write-specific Properties:
     SupportedOutputFormats: ["txt"    "csv"    "xlsx"    "xls"    "parquet"    "parq"]
        DefaultOutputFormat: "txt"

创建数据存储后,可以预览数据而无需将其全部加载到内存中。可以使用 SelectedVariableNames 属性指定相关变量(列),以预览或只读这些变量。

Spreadsheet with two of its columns selected

ds.SelectedVariableNames = {'DepTime','DepDelay'};
preview(ds)
ans =

  8×2 table

    DepTime    DepDelay
    _______    ________

      642         12   
     1021          1   
     2055         20   
     1332         12   
      629         -1   
     1446         63   
      928         -2   
      859         -1 

可以指定数据中表示缺失值的值。在 airlinesmall.csv 中,缺失值由 NA 表示。

ds.TreatAsMissing = 'NA';

如果相关变量的数据存储中的所有数据都放入内存,则可以使用 readall 函数读取。

T = readall(ds);

否则,使用 read 函数读取放入内存的较小子集中的数据。默认情况下,read 函数一次从 TabularTextDatastore 读取 20.000 行。但是,可以通过为 ReadSize 属性分配一个新值来更改此值。

ds.ReadSize = 15000;

在重新读取之前,使用 reset 函数将数据存储重置为初始状态。通过在 while 循环中调用 read 函数,可以对每个数据子集执行中间计算,然后在结束时汇总中间结果。以下代码计算 DepDelay 变量的最大值。

reset(ds)
X = [];
while hasdata(ds)
      T = read(ds);
      X(end+1) = max(T.DepDelay);
end
maxDelay = max(X)
maxDelay =

        1438

如果每个单独文件中的数据都能放入内存,则可以指定每次调用 read 时应读取一个完整文件,而不是特定行数。

reset(ds)
ds.ReadSize = 'file';
X = [];
while hasdata(ds)
      T = read(ds);
      X(end+1) = max(T.DepDelay);
end
maxDelay = max(X);

除了读取数据存储中的数据子集之外,还可以使用 mapreduce 将 map 和 reduce 函数应用于数据存储,或使用 tall 创建一个 tall 数组。有关详细信息,请参阅MapReduce 快速入门使用 tall 数组处理无法放入内存的数据

另请参阅

| | | | | |

相关主题