Main Content

将电子表格数据读取到表中

这best way to represent spreadsheet data in MATLAB® is in a table, which can store a mix of numeric and text data, as well as variable and row names. You can read data into tables interactively or programmatically. To interactively select data, click导入数据tab, in theVariable部分。要通过程序导入数据,请使用以下功能之一:

  • 可读取- 阅读一个工作表。

  • 电子表格datastore— Read multiple worksheets or files.

此示例显示了如何使用两个功能以编程方式导入电子表格数据。样本数据,airlinesmall_subset.xlsx,在1996年至2008年之间每年包含一张表。2003

从工作表中读取所有数据

称呼可读取要读取名为2008的工作表中的所有数据,然后仅显示前10行和列。使用Sheetname-value pair argument. If your data is on the first worksheet in the file, you do not need to specifySheet

t =可读取('airlinesmall_subset.xlsx',,,,'床单',,,,'2008');t(1:10,1:10)
ans =10×10桌Year Month DayofMonth DayOfWeek DepTime CRSDepTime ArrTime CRSArrTime UniqueCarrier FlightNum ____ _____ __________ _________ _______ __________ _______ __________ _____________ _________ 2008 1 3 4 1012 1010 1136 1135 {'WN'} 752 2008 1 4 5 1303 1300 1411 1415 {'WN'} 1161 2008 16 7 2134 2115 2242 2220 {'wn'} 1830 2008 1 7 1 1734 1734 1655 54 30 {'wn'} 302 2008 1 8 2 1750 1755 2018 2018 2018 2035 {'wn'} 1305 2008 1 9 3 640 640 640 645 855 905 {''''Wn'} 896 2008 1 10 4 1943 1945 2039 2040 {'wn'} 120 2008 1 11 5 1303 1303 1305 1401 1400 {'wn'} 1685 2008 13 7 1226 1230 1230 1415 1415 1415 1415 14001337 1340 1623 1630 {'wn'} 730

读取特定工作表的选定范围

从命名的工作表1996,通过指定一个范围,仅读取前5列的10行数据,'a1:e11'。这可读取function returns a 10-by-5 table.

t_selected =可读取('airlinesmall_subset.xlsx',,,,'床单',,,,'1996',,,,'范围',,,,'a1:e11'
t_selected =10×5桌年月DayofmonthDayOfWeek DepTime ____ _____ __________ _________ _______ 1996 1 18 4 2117 1996 1 12 5 1252 1996 1 16 2 1441 1996 1 1 1 2258 1996 1 4 4 1814 1996 1 31 3 1822 1996 1 18 4 729 1996 1 26 5 1704 1996 1 11 4 1858 1996 1 7 7 2100

将变量转换为数据,持续时间或分类

在进口过程中,可读取automatically detects the data types of the variables. However, if your data contains nonstandard dates, durations, or repeated labels, then you can convert those variable to their correct data types. Converting variables to their correct data types lets you perform efficient computations and comparisons and improves memory usage. For instance, represent the variables,,,,,,,,andDayofmonth作为一个datetime变量,独算带作为categorical,,,,andarrdelay作为期间几分钟。

data = t(:,{,{'年',,,,'月',,,,'Dayofmonth',,,,'UniqueCarrier',,,,“ArrDelay”}); data.Date = datetime(data.Year,data.Month,data.DayofMonth); data.UniqueCarrier = categorical(data.UniqueCarrier); data.ArrDelay = minutes(data.ArrDelay);

找到最长延迟的一天,然后显示日期。

ind = find(data.ArrDelay == max(data.ArrDelay)); data.Date(ind)
ans =datetime07-Apr-2008

Read All Worksheets from Spreadsheet File

数据存储对于处理任意大量数据的数据存储非常有用,这些数据分布在多个工作表或多个电子表格文件中。您可以通过数据存储执行数据导入和数据处理。

从工作表的集合中创建一个数据存储airlinesmall_subset.xlsx,选择要导入的变量,然后预览数据。

ds = spreadsheetDatastore('airlinesmall_subset.xlsx');ds.SelectedVariableNames = {'年',,,,'月',,,,'Dayofmonth',,,,'UniqueCarrier',,,,“ArrDelay”};预览(DS)
ans =8×5桌年度月ofmonth kiliqurier arrdelay ____ _____________________________________________ 1996 1 18 {'hp'} 6 1996 1 12 {'hp'} 11 1996 1 16 {'hp'} -13 1996 -13 1996 1 1 1 1 1 1 1 {'HP'} 1 196 1 4 4 4 4{'US'} -9 1996 1 31 {'US'} 9 1996 1 18 {'US'} -2 1996 1 26 {'NW'} -10 -10 -10

在导入数据之前,您可以指定要使用的数据类型。对于此示例,导入独算带作为分类变量。

ds.selectedvariabletypes(4)= {“分类”};

Import data using the读取orread功能。这读取函数要求所有数据拟合到内存,这对于示例数据是正确的。导入后,计算此数据集的最大到达延迟。

alldata = readall(ds);max(alldata.arrdelay)/60
ANS = 15.2333

For large data sets, import portions of the file using theread功能。有关更多信息,请参阅Read Collection or Sequence of Spreadsheet Files

也可以看看

|

Related Topics