matlab导入元胞数组文件,将文本文件中的混合数据块导入表或元胞数组

数据文件概述

样本文件 bigfile.txt 中包含以 ## 开头的注释行。数据排列在五列中:第一列包含指示时间戳的文本。第二、第三和第四列包含数值数据,指示温度、湿度和风速。最后一列包含说明性文本。显示 bigfile.txt 文件内容。

type('bigfile.txt')

## AID = 02476

## YKZ Timestamp Temp Humidity Wind Weather

06-Sep-2013 01:00:006.6894clear

06-Sep-2013 05:00:005.9951clear

06-Sep-2013 09:00:0015.6515mainly clear

06-Sep-2013 13:00:0019.63710mainly clear

06-Sep-2013 17:00:0022.4419mostly cloudy

06-Sep-2013 21:00:0017.3677mainly clear

## BID = 02477

## YVR Timestamp Temp Humidity Wind Weather

09-Sep-2013 01:00:0015.2918clear

09-Sep-2013 05:00:0019.1947n/a

09-Sep-2013 09:00:0018.5944fog

09-Sep-2013 13:00:0020.18115mainly clear

09-Sep-2013 17:00:0020.17717n/a

09-Sep-2013 18:00:0020.07517n/a

09-Sep-2013 21:00:0016.89025mainly clear

## CID = 02478

## YYZ Timestamp Temp Humidity Wind Weather

将数据块导入为表

要将数据导入为表,请将 readtable 与导入选项配合使用。

使用 detectImportOptions 函数,为文件创建一个导入选项对象。使用 DataLines 属性指定数据位置。例如,第 3 行至第 8 行包含第一个数据块。也可选择使用 VariableNames 属性来指定变量的名称。最后,将 readtable 与 opts 对象配合使用,导入第一个数据块。

opts = detectImportOptions('bigfile.txt');

opts.DataLines = [3 8];

opts.VariableNames = {'Timestamp','Temp',...

'Humidity','Wind','Weather'};

T_first = readtable('bigfile.txt',opts)

T_first=6×5 table

Timestamp Temp Humidity Wind Weather

____________________ ____ ________ ____ _________________

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 {'mainly clear' }

06-Sep-2013 13:00:00 19.6 37 10 {'mainly clear' }

06-Sep-2013 17:00:00 22.4 41 9 {'mostly cloudy'}

06-Sep-2013 21:00:00 17.3 67 7 {'mainly clear' }

通过将 DataLines 属性更新为第二个块的位置,读取第二个块。

opts.DataLines = [11 17];

T_second = readtable('bigfile.txt',opts)

T_second=7×5 table

Timestamp Temp Humidity Wind Weather

____________________ ____ ________ ____ ________________

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 {'mainly clear'}

09-Sep-2013 17:00:00 20.1 77 17 {'n/a' }

09-Sep-2013 18:00:00 20 75 17 {'n/a' }

09-Sep-2013 21:00:00 16.8 90 25 {'mainly clear'}

将数据块导入为元胞数组

要以元胞数组形式导入数据,您可以将 readcell 函数与 detectImportOptions 结合使用,或使用 textscan 函数。首先使用 readcell 函数导入数据块,然后使用 textscan 执行相同的导入。

要使用 readcell 函数执行导入,请使用 detectImportOptions 函数为文件创建一个导入选项对象。使用 DataLines 属性指定数据位置。然后,使用 readcell 函数和导入选项对象 opts 执行导入操作。

opts = detectImportOptions('bigfile.txt');

opts.DataLines = [3 8]; % fist block of data

C = readcell('bigfile.txt',opts)

C=6×5 cell array

Columns 1 through 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]} {[ 5]}

{[06-Sep-2013 13:00:00]} {[19.6000]} {[37]} {[10]}

{[06-Sep-2013 17:00:00]} {[22.4000]} {[41]} {[ 9]}

{[06-Sep-2013 21:00:00]} {[17.3000]} {[67]} {[ 7]}

Column 5

{'clear' }

{'clear' }

{'mainly clear' }

{'mainly clear' }

{'mostly cloudy'}

{'mainly clear' }

要使用 textscan 函数执行导入,请使用 N 指定块大小,并使用 formatSpec 指定数据字段的格式。例如,将 '%s' 用于文本变量,将 '%D' 用于日期和时间变量,或将 '%c' 用于分类变量。使用 fopen 打开文件。然后,函数将返回文件标识符 fileID。下一步,使用 textscan 函数读取文件。

N = 6;

formatSpec = '%D %f %f %f %c';

fileID = fopen('bigfile.txt');

读取第一个块并显示变量 Humidity 的内容。

C_first = textscan(fileID,formatSpec,N,'CommentStyle','##','Delimiter','\t')

C_first=1×5 cell array

Columns 1 through 4

{6x1 datetime} {6x1 double} {6x1 double} {6x1 double}

Column 5

{6x1 char}

C_first{3}

ans = 6×1

89

NaN

95

NaN

51

NaN

更新块大小 N,并读取第二个块。显示第五个变量 Weather 的内容。

N = 7;

C_second = textscan(fileID,formatSpec,N,'CommentStyle','##','Delimiter','\t')

C_second=1×5 cell array

Columns 1 through 4

{7x1 datetime} {7x1 double} {7x1 double} {7x1 double}

Column 5

{7x1 char}

C_second{5}

ans = 7x1 char array

'm'

'...'

'm'

'...'

'm'

'...'

'c'

关闭文件。

fclose(fileID);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值