Main Content

Getting Started with Datastore

什么是数据存储?

数据存储中读取一个文件是一个对象or a collection of files or data. The datastore acts as a repository for data that has the same structure and formatting. For example, each file in a datastore must contain data of the same type (such as numeric or text) appearing in the same order, and separated by the same delimiter.

A datastore is useful when:

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

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

您可以根据数据或应用程序的类型创建数据存储。不同类型的数据存储包含与其支持的数据类型有关的属性。万博1manbetx例如,有关MATLAB的列表,请参见下表®datastores. For a complete list of datastores, see选择文件格式或应用程序的数据存储

文件或数据类型 Datastore Type
包含面向列的数据的文本文件,包括CSV文件。 TabularTextDatastore
图像文件,包括支持的格式万博1manbetximreadsuch as JPEG and PNG. 成像
带有支持的Excel的电子表格文件万博1manbetx®格式,例如.xlsx 电子表格datastore
键值对数据是输入或输出的mapreduce KeyValueDatastore
包含面向列的数据的镶链文件。 Parquetdatastore
自定义文件格式。需要提供的读数功能。 提交装甲
Datastore for checkpointing数组。 塔达斯塔尔

从数据存储创建和阅读

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

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"

创建数据存储后,您可以预览数据,而无需将其全部加载到内存中。您可以使用The Import的变量(列)使用选定的variablenames属性要预览或仅读取这些变量。

ds.selectedVariablenames = {'deptime',,,,'depdelay'};预览(DS)
ans = 8×2表deptime depdelay _______ ________ 642 12 1021 1 2055 20 1332 12 629 -1 1446 63 928 -2 859 -1

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

ds.treatasmissing ='na';

如果数据存储中的所有数据都适合记忆的变量,则可以使用读取功能。

t = readall(ds);

Otherwise, read the data in smaller subsets that do fit in memory, using theread功能。默认情况下,read功能从一个TabularTextDatastore一次20,000行。但是,您可以通过将新值分配给读取尺寸财产。

ds.ReadSize = 15000;

Reset the datastore to the initial state before re-reading, using the重置功能。By calling thereadfunction within awhileloop, you can perform intermediate calculations on each subset of data, and then aggregate the intermediate results at the end. This code calculates the maximum value of theDepdelay多变的。

重置(ds)x = [];whilehasdata(ds)t = read(ds);x(end+1)= max(t.depdelay);结尾maxdelay = max(x)
maxDelay = 1438

如果每个文件中的数据适合内存,则可以指定每个调用read应该读取一个完整的文件,而不是特定数量的行。

重置(DS)ds.ReadSize ='文件';x = [];whilehasdata(ds)t = read(ds);x(end+1)= max(t.depdelay);结尾maxdelay = max(x);

在addition to reading subsets of data in a datastore, you can apply map and reduce functions to the datastore usingmapreduce或使用。有关更多信息,请参阅MapReduce入门and高音数据的高阵列

也可以看看

||||||

Related Topics