%% Import Block of Numeric Data from Text File
% This example shows how to read numeric data organized in blocks in a text file. Each block within the file can have a different format. You can read all the blocks as cell arrays, one block at a time, using textscan.
%
%% File Format Overview
% The information in the sample text file, test80211.txt, is the result from a wireless network communication quality test. The sample file consists of four lines of introduction followed by several blocks of data. Each block represents a different environment (for example, mobile, indoor, outdoor) and has the following format:
% Two header lines of description
% The text, Num SNR=, followed by a numeric value, m
% Numeric data organized in a table of m columns and an arbitrary number of rows (The data is comma-delimited.)
% The text, *EOB, denoting the end of the block
% For example, a block of data is formatted like this:
%
% *       Indoor2
% *       SNR Vs test No
% Num SNR=3
% ,-5.00E+00,-4.00E+00,
% 1.00E+00,3.32E-07,9.12E-07
% 2.00E+00,1.49E-07,2.44E-07
% 3.00E+00,6.04E-07,2.53E-07
% 4.00E+00,1.53E-07,4.25E-07
% 5.00E+00,1.82E-07,1.83E-07
% 6.00E+00,6.27E-07,8.21E-07
% 7.00E+00,9.10E-08,1.53E-08
% 8.00E+00,8.73E-07,6.45E-07
% 9.00E+00,4.40E-07,1.33E-07
% *EOB
%
% The numeric data represents error rates over a range of noise levels for a number of independent tests. The first column indicates the test number. To view the entire sample file, type at the command line:
% open test80211.txt
%
%% Open Text File for Reading
% Open the file and create a file identifier.

fileID = fopen('test80211.txt','r');

%% Read Introduction Lines
% Read the four introductory lines, which contain text delimited by a newline character. textscan returns a 1-by-1 cell array containing a 4-by-1 cell array of character vectors.

Intro = textscan(fileID,'%s',4,'Delimiter','\n')

% View the contents of the first cell.

disp(Intro{1})

%% Read Each Block
% For each block, we want to read a header, the numeric value m, column headers for the data, then the data itself. First, initialize the block index.

Block = 1;

%
% Read each block of data in a while loop. The loop executes until the end of the file is reached and ~feof returns false. The textscan function returns the data in each block as a cell array named InputText. Convert each cell array to a numeric array using cell2mat and store the numeric array in a cell array named Data. A cell array allows the storage of different size blocks.

while (~feof(fileID))                               % For each block:                         
   
   fprintf('Block: %s\n', num2str(Block))           % Print block number to the screen
   InputText = textscan(fileID,'%s',2,'delimiter','\n');  % Read 2 header lines
   HeaderLines{Block,1} = InputText{1};
   disp(HeaderLines{Block});                        % Display header lines
   
   InputText = textscan(fileID,'Num SNR = %f');     % Read the numeric value 
                                                    % following the text, Num SNR =
   NumCols = InputText{1};                          % Specify that this is the 
                                                    % number of data columns
   
   FormatString = repmat('%f',1,NumCols);           % Create format string
                                                    % based on the number
                                                    % of columns
   InputText = textscan(fileID,FormatString, ...    % Read data block
      'delimiter',',');
   
   Data{Block,1} = cell2mat(InputText);              
   [NumRows,NumCols] = size(Data{Block});           % Determine size of table
   disp(cellstr(['Table data size: ' ...
      num2str(NumRows) ' x ' num2str(NumCols)]));
   disp(' ');                                       % New line
   
   eob = textscan(fileID,'%s',1,'delimiter','\n');  % Read and discard end-of-block marker 
   Block = Block+1;                                 % Increment block index
end

%
%% Close Text File

fclose(fileID);

%% Total Number of Blocks
% Determine the number of blocks in the file.

NumBlocks = Block-1

%% View Numeric Data
% Display the numeric data in one of the blocks using short scientific notation.
% First, store the current Command Window output display format.

user_format = get(0, 'format');

% Change the display format to short scientific notation.

format shortE

% Display the header lines for the ninth block and the numeric data.

Block = 9;
disp(HeaderLines{Block});
fprintf('SNR        %d        %d\n',Data{Block,1}(1,2:end))
disp(Data{Block,1}(2:end,2:end));

% Restore the original Command Window output display format.

set(0, 'format', user_format);

%
% Copyright 1984-2014 The MathWorks, Inc.