内存中数据的数据存储 - MATLAB - MathWorks 中国 (2024)

内存中数据的数据存储

自 R2020b 起

全页展开

说明

使用 ArrayDatastore 对象管理基于内存中数据创建的数据存储。您可以使用 arrayDatastore 函数创建 ArrayDatastore 对象,指定其属性,然后使用对象函数导入和处理数据。

创建对象

语法

arrds = arrayDatastore(A)

arrds = arrayDatastore(A,Name,Value)

描述

示例

arrds = arrayDatastore(A) 基于存储在内存中的数组 A 创建数据存储 arrds

示例

arrds = arrayDatastore(A,Name,Value) 使用一个或多个名称-值对组参量为 arrds 指定其他参数和属性。例如,通过调用 arrds = arrayDatastore(data,"ReadSize",3) 指定每次调用 read 函数时读取三行数据。

输入参量

全部展开

输入数组,指定为矩阵。

属性

全部展开

ArrayDatastore 属性描述数据存储对象中内存数据的格式,并控制如何从数据存储中读取数据。创建数据存储对象时,可以使用名称-值对组参量指定 ArrayDatastore 属性的值。要在创建对象后查看或修改属性,请使用圆点表示法。

在调用 read 函数时要读取的数据量,指定为由 'ReadSize' 和正整数组成的以逗号分隔的对组。每次调用 read 最多读取 ReadSize 行。如果为 'ReadSize' 指定的值超过输入数据的行数,则 read 将读取数据存储对象中的所有行。

'ReadSize' 的默认值是 1

数据类型: double

在调用 read 函数时要在其中读取数据的维度,指定为由 'IterationDimension' 和正整数组成的以逗号分隔的对组。例如,'IterationDimension',2 使 read 从数据存储对象返回列向数据。'IterationDimension' 的默认值为 1,这使得 read 返回行向数据。

如果将 'OutputType' 属性的值指定为 'same',则 'IterationDimension' 必须设置为值 1。

如果在创建 ArrayDatastore 对象后修改 'IterationDimension' 的值,则 MATLAB® 会将数据存储重置为未读状态。

数据类型: double

输出数据类型,指定为以逗号分隔的对组,该对组由 'OutputType' 和下列值之一组成:

  • 'cell' - 以 n×1 元胞数组形式返回数据。例如,如果 A 是数值数组,ReadSize3,则 read 返回由数值数据组成的 3×1 元胞数组。

  • 'same' - 返回与输入数组 A 相同的数据类型。例如,如果 A 是数值数组,则 read 返回数值数组。

OutputType 的值决定 previewreadreadall 函数返回的数据类型。

如果在创建 ArrayDatastore 对象后修改 'OutputType' 的值,则 MATLAB 会将数据存储重置为未读状态。

数据类型: char | string

对象函数

hasdata确定是否有数据可读取
numpartitions数据存储分区数
partition划分数据存储
preview预览数据存储中的数据子集
read读取数据存储中的数据
readall读取数据存储中的所有数据
reset将数据存储重置为初始状态
transform变换数据存储
combine合并来自多个数据存储的数据
shuffle对数据存储中的所有数据进行乱序处理
subset创建数据存储或 FileSet 的子集

示例

全部折叠

创建 ArrayDatastore 对象

打开实时脚本

从矩阵中创建一个 ArrayDatastore 对象,然后读取数据存储中的所有数据。

创建一个矩阵。

A = magic(10)
A = 10×10 92 99 1 8 15 67 74 51 58 40 98 80 7 14 16 73 55 57 64 41 4 81 88 20 22 54 56 63 70 47 85 87 19 21 3 60 62 69 71 28 86 93 25 2 9 61 68 75 52 34 17 24 76 83 90 42 49 26 33 65 23 5 82 89 91 48 30 32 39 66 79 6 13 95 97 29 31 38 45 72 10 12 94 96 78 35 37 44 46 53 11 18 100 77 84 36 43 50 27 59

从矩阵中创建一个 ArrayDatastore 对象。

arrds = arrayDatastore(A)
arrds = ArrayDatastore with properties: ReadSize: 1 IterationDimension: 1 OutputType: "cell"

读取数据存储中的所有数据。

readall(arrds)
ans=10×1 cell array {[ 92 99 1 8 15 67 74 51 58 40]} {[ 98 80 7 14 16 73 55 57 64 41]} {[ 4 81 88 20 22 54 56 63 70 47]} {[ 85 87 19 21 3 60 62 69 71 28]} {[ 86 93 25 2 9 61 68 75 52 34]} {[ 17 24 76 83 90 42 49 26 33 65]} {[ 23 5 82 89 91 48 30 32 39 66]} {[ 79 6 13 95 97 29 31 38 45 72]} {[ 10 12 94 96 78 35 37 44 46 53]} {[11 18 100 77 84 36 43 50 27 59]}

返回与输入数组相同的数据类型

打开实时脚本

返回与输入数组相同的数据类型,而不是以 n×1 元胞数组形式返回数据。

基于电子表格文件 counties.xlsx 创建一个表。导入该电子表格中第五列到第十列的所有行。

T = readtable("counties.xlsx","Range",[1,5,67,10])
T=66×6 table CountyName State StateName Population2010 HousingUnits2010 LandArea _______________________ ______ _______________ ______________ ________________ __________ {'Fairfield County' } {'CT'} {'Connecticut'} 9.1683e+05 3.6122e+05 1.6185e+09 {'Hartford County' } {'CT'} {'Connecticut'} 8.9401e+05 3.7425e+05 1.9039e+09 {'Litchfield County' } {'CT'} {'Connecticut'} 1.8993e+05 87550 2.3842e+09 {'Middlesex County' } {'CT'} {'Connecticut'} 1.6568e+05 74837 9.5649e+08 {'New Haven County' } {'CT'} {'Connecticut'} 8.6248e+05 3.62e+05 1.5657e+09 {'New London County' } {'CT'} {'Connecticut'} 2.7406e+05 1.2099e+05 1.722e+09 {'Tolland County' } {'CT'} {'Connecticut'} 1.5269e+05 57963 1.0624e+09 {'Windham County' } {'CT'} {'Connecticut'} 1.1843e+05 49073 1.3284e+09 {'Androscoggin County'} {'ME'} {'Maine' } 1.077e+05 49090 1.2119e+09 {'Aroostook County' } {'ME'} {'Maine' } 71870 39529 1.7279e+10 {'Cumberland County' } {'ME'} {'Maine' } 2.8167e+05 1.3866e+05 2.1633e+09 {'Franklin County' } {'ME'} {'Maine' } 30768 21709 4.3942e+09 {'Hanco*ck County' } {'ME'} {'Maine' } 54418 40184 4.11e+09 {'Kennebec County' } {'ME'} {'Maine' } 1.2215e+05 60972 2.2469e+09 {'Knox County' } {'ME'} {'Maine' } 39736 23744 9.4569e+08 {'Lincoln County' } {'ME'} {'Maine' } 34457 23493 1.1806e+09 ⋮

基于该表创建一个数据存储。将 'OutputType' 设置为 'same',以返回与输入表相同的数据类型。

arrds = arrayDatastore(T,"OutputType","same")
arrds = ArrayDatastore with properties: ReadSize: 1 IterationDimension: 1 OutputType: "same"

预览数据存储中的数据。

preview(arrds)
ans=8×6 table CountyName State StateName Population2010 HousingUnits2010 LandArea _____________________ ______ _______________ ______________ ________________ __________ {'Fairfield County' } {'CT'} {'Connecticut'} 9.1683e+05 3.6122e+05 1.6185e+09 {'Hartford County' } {'CT'} {'Connecticut'} 8.9401e+05 3.7425e+05 1.9039e+09 {'Litchfield County'} {'CT'} {'Connecticut'} 1.8993e+05 87550 2.3842e+09 {'Middlesex County' } {'CT'} {'Connecticut'} 1.6568e+05 74837 9.5649e+08 {'New Haven County' } {'CT'} {'Connecticut'} 8.6248e+05 3.62e+05 1.5657e+09 {'New London County'} {'CT'} {'Connecticut'} 2.7406e+05 1.2099e+05 1.722e+09 {'Tolland County' } {'CT'} {'Connecticut'} 1.5269e+05 57963 1.0624e+09 {'Windham County' } {'CT'} {'Connecticut'} 1.1843e+05 49073 1.3284e+09

指定 ArrayDatastore 的读取大小

打开实时脚本

为 MAT 文件变量创建一个数据存储,然后从包含不同 ReadSize 值的文件中读取数据。

将 MAT 文件 BostonWeatherData.mat 加载到工作区中。

load 'BostonWeatherData.mat'

weatherData 变量创建一个数据存储。将 ReadSize 设置为 10 行。ReadSize 的值决定每次调用 read 函数时从数据存储中读取多少行数据。将 'OutputType' 设置为 'same',以返回与输入数组相同的数据类型。

arrds = arrayDatastore(weatherData,"ReadSize",10, "OutputType","same")
arrds = ArrayDatastore with properties: ReadSize: 10 IterationDimension: 1 OutputType: "same"

从数据存储读取数据。

data1 = read(arrds)
data1=10×3 timetable Time TemperatureF Humidity Events ___________ ____________ ________ ____________ 01-Jul-2015 72 78 Thunderstorm 02-Jul-2015 72 60 None 03-Jul-2015 70 56 None 04-Jul-2015 67 75 None 05-Jul-2015 72 67 None 06-Jul-2015 74 69 None 07-Jul-2015 75 77 Rain 08-Jul-2015 79 68 Rain 09-Jul-2015 66 77 Rain 10-Jul-2015 69 74 Rain 

ReadSize 属性值设置为 30,并从数据存储中读取。第二次调用 read 函数将从数据存储中读取接下来的 30 行。

arrds.ReadSize = 30;

从数据存储读取数据。

data2 = read(arrds)
data2=30×3 timetable Time TemperatureF Humidity Events ___________ ____________ ________ ______ 11-Jul-2015 76 49 None 12-Jul-2015 81 54 None 13-Jul-2015 72 81 None 14-Jul-2015 74 72 Rain 15-Jul-2015 75 87 Rain 16-Jul-2015 64 65 None 17-Jul-2015 68 72 None 18-Jul-2015 71 81 Rain 19-Jul-2015 81 73 Rain 20-Jul-2015 81 62 None 21-Jul-2015 76 66 None 22-Jul-2015 77 58 None 23-Jul-2015 75 52 None 24-Jul-2015 74 60 Rain 25-Jul-2015 66 81 None 26-Jul-2015 71 79 Rain ⋮

指定迭代维度

打开实时脚本

您可以选择要在其中读取 ArrayDatastore 中的数据的维度。例如,您可以通过沿第四个维度读取来读取其数据存储在 ArrayDatastore 中的视频帧。

加载视频数据。基于文件 xylophone.mp4 创建一个 VideoReader 对象。

v = VideoReader('xylophone.mp4');

VideoReader 对象中的所有视频帧读入工作区中。

allFrames = read(v);

基于您读取的帧创建一个数据存储。将 'IterationDimension' 设置为 4 以沿第四个维度读取数据。将 'OutputType' 设置为 'cell' 以元胞数组形式返回数据。将 'ReadSize' 设置为 4 以便在每次调用 read 函数时读取四个视频帧。

arrds = arrayDatastore(allFrames,"IterationDimension",4,"OutputType","cell","ReadSize",4)
arrds = ArrayDatastore with properties: ReadSize: 4 IterationDimension: 4 OutputType: "cell"

读取数据存储中的前四个视频帧,并将其显示为矩形分块图像。

frames = read(arrds);imout = imtile(frames);imshow(imout)

内存中数据的数据存储 - MATLAB- MathWorks 中国 (1)

提示

  • 您可以使用 combinetransform 函数,对 ArrayDatastore 对象与包含磁盘数据的数据存储(如 ImageDatastoreTabularTextDatastore 对象)进行合并和变换。

版本历史记录

在 R2020b 中推出

另请参阅

datastore | matlab.io.Datastore

MATLAB 命令

您点击的链接对应于以下 MATLAB 命令:

 

请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。

内存中数据的数据存储 - MATLAB- MathWorks 中国 (2)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

Europe

Asia Pacific

Contact your local office

内存中数据的数据存储 - MATLAB
- MathWorks 中国 (2024)
Top Articles
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 5898

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.