目录
readmatrix函数的功能是从文件中读取矩阵。
语法
A = readmatrix(filename)
A = readmatrix(filename,opts)
A = readmatrix(___,Name,Value)
说明
A = readmatrix(filename) 通过从文件中读取列向数据来创建数组。readmatrix 函数可自动检测文件的导入参数。
readmatrix 基于文件的扩展名确定文件格式:
-
.txt、.dat 或 .csv(适用于带分隔符的文本文件)
-
.xls、.xlsb、.xlsm、.xlsx、.xltm、.xltx 或 .ods(适用于电子表格文件)
对于包含数值和文本混合数据的文件,默认情况下,readmatrix 将数据作为数值数组导入。
A = readmatrix(filename,opts) 还使用导入选项 opts。
A = readmatrix(___,Name,Value) 基于文件创建一个数组,并通过一个或多个名称-值对组参数指定其他选项。需要在上述语法的输入参数之后指定名称-值对组。
要为数据设置特定的导入选项,可以使用opts对象,也可以指定名称-值对组。当指定除 opts 之外的名称-值对组时,readmatrix仅支持下列名称-值对组:
-
文本文件 - DateLocale、Encoding
-
电子表格文件 - Sheet、UseExcel
示例
从文本文件中读取矩阵
显示 basic_matrix.txt的内容,然后将数据导入矩阵。
type basic_matrix.txt
6,8,3,1
5,4,7,3
1,6,7,10
4,2,8,2
2,7,5,9
M = readmatrix('basic_matrix.txt')
M = 5×4
6 8 3 1
5 4 7 3
1 6 7 10
4 2 8 2
2 7 5 9
从电子表格文件中读取矩阵
将数值数据从 basic_matrix.xls 导入矩阵。
M = readmatrix('basic_matrix.xls')
M = 5×4
6 8 3 1
5 4 7 3
1 6 7 10
4 2 8 2
2 7 5 9
使用导入选项从指定的工作表和范围中读取矩阵
预览电子表格文件中的数据,并以矩阵形式从指定的工作表和范围中导入数值数据。
电子表格文件 airlinesmall_subset.xlsx 包含 1996 年至 2008 年间多个工作表中的数据。每个工作表都包含给定年份的数据。预览文件 airlinesmall_subset.xlsx 中的数据。默认情况下,preview 函数显示第一个工作表中的数据。文件中的前八个变量包含数值数据。
opts = detectImportOptions('airlinesmall_subset.xlsx');
preview('airlinesmall_subset.xlsx',opts)
ans=8×29 table
Year Month DayofMonth DayOfWeek DepTime CRSDepTime ArrTime CRSArrTime UniqueCarrier FlightNum TailNum ActualElapsedTime CRSElapsedTime AirTime ArrDelay DepDelay Origin Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted CarrierDelay WeatherDelay SDelay SecurityDelay LateAircraftDelay
____ _____ __________ _________ _______ __________ _______ __________ _____________ _________ __________ _________________ ______________ _______ ________ ________ _______ _______ ________ ______ _______ _________ ________________ ________ ____________ ____________ __________ _____________ _________________
1996 1 18 4 2117 2120 2305 2259 {'HP'} 415 {'N637AW'} 108 99 85 6 -3 {'COS'} {'PHX'} 551 5 18 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 12 5 1252 1245 1511 1500 {'HP'} 610 {'N905AW'} 79 75 58 11 7 {'LAX'} {'PHX'} 370 3 18 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 16 2 1441 1445 1708 1721 {'HP'} 211 {'N165AW'} 87 96 74 -13 -4 {'RNO'} {'PHX'} 601 4 9 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 1 1 2258 2300 2336 2335 {'HP'} 1245 {'N183AW'} 38 35 20 1 -2 {'TUS'} {'PHX'} 110 6 12 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 4 4 1814 1814 1901 1910 {'US'} 683 {'N963VJ'} 47 56 34 -9 0 {'DTW'} {'PIT'} 201 6 7 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 31 3 1822 1820 1934 1925 {'US'} 757 {'N912VJ'} 72 65 52 9 2 {'PHL'} {'PIT'} 267 6 14 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 18 4 729 730 841 843 {'US'} 1564 {'N941VJ'} 72 73 58 -2 -1 {'DCA'} {'PVD'} 357 3 11 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 26 5 1704 1705 1829 1839 {'NW'} 1538 {'N960N' } 85 94 69 -10 -1 {'DTW'} {'RIC'} 456 3 13 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
配置 opts 对象中的值,以便从名为 '2007' 的工作表中导入前五个变量的 10 行。
opts.Sheet = '2007';
opts.SelectedVariableNames = [1:5];
opts.DataRange = '2:11';
M = readmatrix('airlinesmall_subset.xlsx',opts)
M = 10×5
2007 1 2 2 711
2007 1 3 3 652
2007 1 4 4 1116
2007 1 5 5 825
2007 1 7 7 1411
2007 1 8 1 1935
2007 1 9 2 2005
2007 1 11 4 1525
2007 1 12 5 1133
2007 1 13 6 922
从指定的工作表和范围中读取矩阵
预览电子表格文件中的数据,并以矩阵形式从指定的工作表和范围中导入数值数据。
电子表格文件 airlinesmall_subset.xlsx 包含 1996 年至 2008 年间多个工作表中的数据。每个工作表都包含给定年份的数据。预览文件 airlinesmall_subset.xlsx 中的数据。默认情况下,preview 函数显示第一个工作表中的数据。文件中的前八个变量包含数值数据。
opts = detectImportOptions('airlinesmall_subset.xlsx');
preview('airlinesmall_subset.xlsx',opts)
ans=8×29 table
Year Month DayofMonth DayOfWeek DepTime CRSDepTime ArrTime CRSArrTime UniqueCarrier FlightNum TailNum ActualElapsedTime CRSElapsedTime AirTime ArrDelay DepDelay Origin Dest Distance TaxiIn TaxiOut Cancelled CancellationCode Diverted CarrierDelay WeatherDelay SDelay SecurityDelay LateAircraftDelay
____ _____ __________ _________ _______ __________ _______ __________ _____________ _________ __________ _________________ ______________ _______ ________ ________ _______ _______ ________ ______ _______ _________ ________________ ________ ____________ ____________ __________ _____________ _________________
1996 1 18 4 2117 2120 2305 2259 {'HP'} 415 {'N637AW'} 108 99 85 6 -3 {'COS'} {'PHX'} 551 5 18 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 12 5 1252 1245 1511 1500 {'HP'} 610 {'N905AW'} 79 75 58 11 7 {'LAX'} {'PHX'} 370 3 18 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 16 2 1441 1445 1708 1721 {'HP'} 211 {'N165AW'} 87 96 74 -13 -4 {'RNO'} {'PHX'} 601 4 9 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 1 1 2258 2300 2336 2335 {'HP'} 1245 {'N183AW'} 38 35 20 1 -2 {'TUS'} {'PHX'} 110 6 12 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 4 4 1814 1814 1901 1910 {'US'} 683 {'N963VJ'} 47 56 34 -9 0 {'DTW'} {'PIT'} 201 6 7 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 31 3 1822 1820 1934 1925 {'US'} 757 {'N912VJ'} 72 65 52 9 2 {'PHL'} {'PIT'} 267 6 14 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 18 4 729 730 841 843 {'US'} 1564 {'N941VJ'} 72 73 58 -2 -1 {'DCA'} {'PVD'} 357 3 11 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
1996 1 26 5 1704 1705 1829 1839 {'NW'} 1538 {'N960N' } 85 94 69 -10 -1 {'DTW'} {'RIC'} 456 3 13 0 {0x0 char} 0 {0x0 char} {0x0 char} {0x0 char} {0x0 char} {0x0 char}
从名为 '2007' 的工作表中导入前5个变量的10行。
M = readmatrix('airlinesmall_subset.xlsx','Sheet','2007','Range','A2:E11')
M = 10×5
2007 1 2 2 711
2007 1 3 3 652
2007 1 4 4 1116
2007 1 5 5 825
2007 1 7 7 1411
2007 1 8 1 1935
2007 1 9 2 2005
2007 1 11 4 1525
2007 1 12 5 1133
2007 1 13 6 922
要读取的文件的名称,指定为字符向量或字符串标量。根据文件的位置,filename 可以采用下列形式之一。
-
如果 filename 包含文件扩展名,则导入函数基于扩展名确定文件格式。否则,必须指定 'FileType' 名称-值对组参数以指示文件类型。
-
在安装了 Microsoft® Excel® 软件的 Windows® 系统上,导入函数读取Excel版本可识别的任何 Excel 电子表格文件格式。
-
如果系统没有Windows版Excel或者正在使用 MATLAB Online™,则导入函数在UseExcel属性设置为false的情况下运行,并且只读取 .xls, .xlsx, .xlsm, .xltx, and .xltm 文件。
-
对于分隔的文本文件,导入函数将文件中的空字段转换为 NaN(对于数值变量)或空字符向量(对于文本变量)。文本文件中的所有行必须有相同数量的分隔符。导入函数会忽略文件中的无用空白。
文件导入选项,指定为detectImportOptions函数创建的
SpreadsheetImportOptions、DelimitedTextImportOptions 或 FixedWidthImportOptions 对象
opts对象包含控制数据导入过程的属性。有关每个对象的属性的详细信息参考相应的对象页。
文件类型 | 输出 |
---|---|
电子表格文件 | SpreadsheetImportOptions对象 |
文本文件 | DelimitedTextImportOptions对象 |
等宽文本文件 | FixedWidthImportOptions对象 |
注:readmatrix在 R2019a 以上的版本中才会有,低于此版本的是没有这个函数的。