%% Import Data from Nonrectangular Text File
% This example shows how to import data from a nonrectangular file. If your data does not have regular columns and rows, but it has a repeated pattern, then you can import your data in one of these ways:
% readtable function with import options - use when you need additional control over the import parameters  and need your output in the form of a table.
% textscan -  use when you can describe your data using the format specifiers and need your output in a cell array.
%% Overview of Data File
% The file nonrect.dat contains data in a nonrectangular format. Display the contents of this file by using the type function.

type('nonrect.dat')

%
%% Import Nonrectangular Data using Import Options
% Import options enable you to specify properties that give additional control over the import process. The data chunks in this data file contain a mixture of text and numbers. For instance, line 2 contains v1=12.67, which can be separated into two parts: v1 and 12.67. Therefore, create an import options object for nonrect.dat that contains two variables using the detectImportOptions function. The detectImportOptions function automatically detects the delimiter  as '=' and variable types as 'char' and 'double'.

opts = detectImportOptions('nonrect.dat','NumVariables',2) 

% Update the import option parameters. Specify the variable names and the location of the first block of data.

opts.VariableNames = {'VarNum','VarValue'};
opts.DataLines = [2 4];

% Read the first block of data.

T_first = readtable('nonrect.dat',opts)

% Update the opts object with the location of the second block and then read the data.

opts.DataLines = [7 9];
T_second = readtable('nonrect.dat',opts)

%
%% Import Nonrectangular Data using textscan
% Open the file to read using the fopen function and create a format specifier to read the file using textscan. The fopen function returns a file identifier, fileID, that textscan calls to read from the file.

fileID = fopen('nonrect.dat');

% Describe the pattern of the file data using format specifiers and delimiter parameters. Typical format specifiers include '%s' for a character vector, '%d' for an integer, or '%f' for a floating-point number. To import nonrect.dat, use the format specifier '%*s' to tell textscan to skip the rows that contain begin and end. Include the literals 'v1=', 'v2=', and 'v3=' as part of the format specifiers, so that textscan ignores those literals as well.

formatSpec = '%*s v1=%f v2=%f v3=%f %*s';

% Import the data using textscan. Pass the file identifier and formatSpec as inputs. Since each data field is on a new line, the delimiter is a newline character ('\n'). To combine all the floating-point data into a single array, set the CollectOutput name-value pair argument to true. textscan returns the cell array, C.

C = textscan(fileID,formatSpec,...            
                'Delimiter', '\n', ...
                'CollectOutput', true)            

% View the contents of C.

celldisp(C)   

% Close the file.

fclose(fileID);

% Copyright 2018 The MathWorks, Inc.