Main Content

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

本示例从文本文件中读取一个包含文本和数值的混合数据块,然后将该数据块导入到表中或元胞数组中。

数据文件概述

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

type('bigfile.txt')
## A	ID = 02476
## YKZ 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
## B	ID = 02477
## YVR 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.0	75	17	n/a
09-Sep-2013 21:00:00	16.8	90	25	mainly clear
## C	ID = 02478
## YYZ Timestamp Temp Humidity Wind Weather

将数据块导入为表

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

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

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
    {[06-Sep-2013 01:00:00]}    {[ 6.6000]}    {[89]}    {[ 4]}    {'clear'        }
    {[06-Sep-2013 05:00:00]}    {[ 5.9000]}    {[95]}    {[ 1]}    {'clear'        }
    {[06-Sep-2013 09:00:00]}    {[15.6000]}    {[51]}    {[ 5]}    {'mainly clear' }
    {[06-Sep-2013 13:00:00]}    {[19.6000]}    {[37]}    {[10]}    {'mainly clear' }
    {[06-Sep-2013 17:00:00]}    {[22.4000]}    {[41]}    {[ 9]}    {'mostly cloudy'}
    {[06-Sep-2013 21:00:00]}    {[17.3000]}    {[67]}    {[ 7]}    {'mainly clear' }

要使用 textscan 函数执行导入,请使用 N 指定块大小,并使用 formatSpec 指定数据字段的格式。例如,将 '%s' 用于文本变量,将 '%D' 用于日期时间变量,或将 '%c' 用于分类变量。将 'DateLocale' 名称-值参量设置为 'en_US',以确保月份名称用英语解释。使用 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','DateLocale','en_US')
C_first=1×5 cell array
    {6×1 datetime}    {6×1 double}    {6×1 double}    {6×1 double}    {6×1 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','DateLocale','en_US')
C_second=1×5 cell array
    {7×1 datetime}    {7×1 double}    {7×1 double}    {7×1 double}    {7×1 char}

C_second{5}
ans = 7×1 char array
    'm'
    '↵'
    'm'
    '↵'
    'm'
    '↵'
    'c'

关闭文件。

fclose(fileID);

另请参阅

| | | |

相关主题