此示例从文本文件读取混合文本块和数字数据,然后将数据块导入表或单元格数组。
示例文件bigfile.txt.
包含以##开头的评论线。数据以五列排列:第一列包含指示时间戳的文本。第二个,第三和第四列包含数字数据,指示温度,湿度和风速。最后一列包含描述性文本。显示文件的内容bigfile.txt.
。
类型('bigfile.txt')
## A ID = 02476 ## YKZ时间戳临时湿气风天气06-SEP-2013 01:00:00 6.6 89 4 CLEAR 06-SEP-2013 05:00:00 5.9 95 1清除06-SEP-2013 09:00:00 15.6 51 5主要清除06-SEP-2013 13:00:00 19.6 37 10主要清除06-Sep-2013 17:00:00 22.4 41 9大多数多云06-2013 21:00:00 173 67 7主要清除## b id = 02477 ## YVR时间戳临时风湿天气09-SEP-2013 01:00:00 15.2 91 8清除09-SEP-2013 05:00:00 19.1 94 7 N / A 09-SEP-2013 09:00:00 18.5 94 4 FOG 09-SEP-2013 13:00:00 20.1 81 15主要清除09-SEP-2013 17:00:00 20.1 77 17 17 N / A 09-SEP-2013 18:00:00 20.0 75 17 17 N / A 09-SEP-2013 21:00:00 16.8 90 25主要清除## C ID = 02478 ## YYZ时间戳临时风风光
将数据作为表导入,使用可阅读
具有导入选项。
使用使用的文件为文件创建一个导入选项对象侦探
功能。使用该数据指定数据的位置Datalines.
财产。例如,线条3.
通过8.
包含第一个数据块。(可选)您可以使用使用的变量的名称variablenames.
财产。最后使用第一数据块可阅读
与之选择
目的。
opts = detectimportoptions('bigfile.txt');opts.datalines = [3 8];opts.variablenames = {'时间戳'那'temp'那......'湿度'那'风'那'天气'};t_first = readtable('bigfile.txt',选择)
t_first =6×5表时间戳临时湿度风天气____________________ _______________________________ 06-sep-2013 01:00:00 6.6 89 4 {'clear'} 06-sep-2013 05:00:00 5.9 95 1 {'clear'} 06-sep-2013 09:00:00 15.6 51 5 {'主要是清除'} 06-SEP-2013 13:00:00 19.6 37 10 {'主要清除'} 06-SEP-2013 17:00:00 22.4 41 9 {'主要是Cloudy'} 06-Sep-2013 21:00:00 17.3 67 7 {'主要是清晰的'}
通过更新读取第二个块Datalines.
属性到第二个块的位置。
opts.datalines = [11 17];t_second = readtable('bigfile.txt',选择)
t_second =.7×5表时间戳临时湿度风天气_________________________________________________ 09-sep-2013 01:00:00 15.2 91 8 {'clear'} 09-sep-2013 05:00:00 19.1 94 7 {'n / a'} 09-Sep-2013 09:00:00 18.5 94 4 {'FOG'} 09-SEP-2013 13:00:00 20.1 81 15 {'主要清除'} 09-SEP-2013 17:00:00 20.1 77 17 17 {'n / a'} 09-sep-2013 18:00:00 20 75 17 17 {'n / a'} 09-sep-2013 21:00:00 16.8 90 25 {'主要是清晰的'}
您可以使用使用该数据作为单元格数组导入数据readcell.
功能侦探
,或使用textscan.
功能。首先使用该第一导入数据块readcell.
功能,然后通过使用执行相同的导入textscan.
。
要使用ReadCell函数执行导入,请使用该文件为文件创建导入选项对象侦探
功能。使用该数据指定数据的位置Datalines.
财产。然后,使用该执行导入操作readcell.
功能和导入选项对象选择
。
opts = detectimportoptions('bigfile.txt');opts.datalines = [3 8];%拳头数据块c = readcell('bigfile.txt',选择)
C =6×5个单元阵列列1到4 {[06-sep-2013 01:00:00]} {[6.6000]} {[89]} {[4]} {[06-sep-2013 05:00:00]} {[5.9000] {[95]} {[1]} {[06-sep-2013 09:00:00]} {[15.6000] {[51]} {[51]} {[5]} {[5]} {[06-sep-2013 13:00:00]} {[19.6000]} {[37]} {[10]} {[06-sep-2013 17:00:00] {[22.4000]} {[41]} {[9]} {[06-ep-2013 21:00:00]} {[17.3000]} {[67]} {[7]} {[7]}第5列5 {'clear'} {'clear'} {'主要是清除'} {'主要是清晰的'} {'大多是多云的'} {'主要是清楚'}
使用此导入使用textscan.
函数,指定块的大小N
和数据字段的格式使用formatspec.
。例如,使用'%s'
对于文本变量,'%d'
对于日期和时间变量,或'%C'
对于分类变量。设定'datelocale'
名称 - 值参数'en_us'
确保月份的名称以英语解释。用富豪
打开文件。然后函数返回文件标识符,fileid.
。接下来,使用该文件读取文件textscan.
功能。
n = 6;formatspec =.'%d%f%f%c';fileid = fopen('bigfile.txt');
读取第一个块并显示变量的内容湿度
。
c_first = textscan(fileid,formatspec,n,'CommentStyle'那'##'那'delimiter'那'\ t'那'datelocale'那'en_us')
c_first =1×5个单元阵列列1到4 {6x1 datetime} {6x1 double} {6x1 double} {6x1 double}列5 {6x1 char}
c_first {3}
ans =.6×189 NaN 95 NaN 51纳米
更新块大小n,然后读取第二个块。显示第五变量的内容天气
。
n = 7;c_second = textscan(fileid,formatspec,n,'CommentStyle'那'##'那'delimiter'那'\ t'那'datelocale'那'en_us')
c_second =.1×5个单元阵列列1到4 {7x1 dateTime} {7x1 double} {7x1 double} {7x1 double}列5 {7x1 char}
c_second {5}
ans =.7x1 char array.'m''...''m'...''m'...''c'
关闭文件。
fclose(fileid);