%% Import Block of Mixed Data from Text File into Table or Cell Array
% This example reads a block of mixed text and numeric data from a text file, and then imports the block of data into a table or a cell array.
%
%% Data File Overview
% The sample file bigfile.txt contains commented lines beginning with ##. The data is arranged in five columns: The first column contains text indicating timestamps. The second, third, and fourth columns contain numeric data indicating temperature, humidity and wind speed. The last column contains descriptive text. Display the contents of the file bigfile.txt.

type('bigfile.txt')

%
%% Import Block of Data as Table
% To import the data as a table, use readtable with import options.
% Create an import options object for the file using the detectImportOptions function. Specify the location of the data using the DataLines property. For example, lines 3 through 8 contain the first block of data. Optionally, you can specify the names of the variables using the VariableNames property. Finally import the first block of data using readtable with the opts object.

opts = detectImportOptions('bigfile.txt'); 
opts.DataLines = [3 8];
opts.VariableNames = {'Timestamp','Temp',...
                      'Humidity','Wind','Weather'};
T_first = readtable('bigfile.txt',opts) 

% Read the second block by updating the DataLines property to the location of the second block.

opts.DataLines = [11 17];
T_second = readtable('bigfile.txt',opts)

%
%% Import Block of Data as Cell Array
% You can import the data as cell array using the readcell function with detectImportOptions, or by using the textscan function. First import the block of data using the readcell function and then perform the same import by using textscan.
% To perform the import using the readcell function, create an import options object for the file using the detectImportOptions function. Specify the location of the data using the DataLines property. Then, perform the import operation using the readcell function and import options object opts.

opts = detectImportOptions('bigfile.txt'); 
opts.DataLines = [3 8]; % fist block of data
C = readcell('bigfile.txt',opts)

% To perform the import using the textscan function, specify the size of block using N and the format of the data fields using formatSpec. For example, use '%s' for text variables, '%D' for date and time variables, or '%c' for categorical variables. Use fopen to open the file. The function then returns a file identifier, fileID. Next, read from the file by using the textscan function.

N = 6;
formatSpec = '%D %f %f %f %c';
fileID = fopen('bigfile.txt');

% Read the first block and display the contents of the variable Humidity.

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

% Update the block size N, and read the second block. Display the contents of the fifth variable Weather.

N = 7;
C_second = textscan(fileID,formatSpec,N,'CommentStyle','##','Delimiter','\t')
C_second{5}

% Close the file.

fclose(fileID);

% Copyright 2018 The MathWorks, Inc.