Main Content

テキスト ファイルから table または cell 配列に混在データのブロックをインポート

この例では、テキスト ファイルから混在するテキストと数値データのブロックを読み取り、データのブロックを table または cell 配列にインポートします。

データ ファイルの概要

サンプル ファイル bigfile.txt には、## で始まるコメント行が含まれています。データは 5 列に配置されています。最初の列には、タイムスタンプを示すテキストが含まれています。2 番目、3 番目、4 番目の列には、温度、湿度、風速を示す数値データがあります。最後の列には、説明テキストが含まれています。ファイル 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

データのブロックを table としてインポート

データを table としてインポートするには、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 プロパティを 2 番目のブロックの位置に更新して、2 番目のブロックを読み取ります。

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'}

データのブロックを cell 配列としてインポート

関数 readcelldetectImportOptions とともに使用するか、関数 textscan を使用して、データを cell 配列としてインポートできます。最初に関数 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'、categorical 変数には '%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 を更新し、2 番目のブロックを読み取ります。5 番目の変数 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);

参考

| | | |

関連するトピック