Main Content

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












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')
Intro = 1x1 cell array
    {4x1 cell}

View the contents of the first cell.

    {'*CCX'                      }
    {'*CCX WiFi conformance test'}
    {'*CCX BER Results'          }
    {'*CCX'                      }

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
   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
Block: 1
    {'*       Mobile1'       }
    {'*       SNR Vs test No'}
    {'Table data size: 30 x 19'}
Block: 2
    {'*       Mobile2'       }
    {'*       SNR Vs test No'}
    {'Table data size: 30 x 9'}
Block: 3
    {'*       Mobile3'       }
    {'*       SNR Vs test No'}
    {'Table data size: 31 x 15'}
Block: 4
    {'*       Mobile4'       }
    {'*       SNR Vs test No'}
    {'Table data size: 28 x 19'}
Block: 5
    {'*       Mobile5'       }
    {'*       SNR Vs test No'}
    {'Table data size: 32 x 18'}
Block: 6
    {'*       Mobile6'       }
    {'*       SNR Vs test No'}
    {'Table data size: 30 x 19'}
Block: 7
    {'*       Mobile7'       }
    {'*       SNR Vs test No'}
    {'Table data size: 30 x 11'}
Block: 8
    {'*       Mobile8'       }
    {'*       SNR Vs test No'}
    {'Table data size: 20 x 18'}
Block: 9
    {'*       Indoor0'       }
    {'*       SNR Vs test No'}
    {'Table data size: 9 x 3'}
Block: 10
    {'*       Indoor1'       }
    {'*       SNR Vs test No'}
    {'Table data size: 22 x 6'}
Block: 11
    {'*       Indoor2'       }
    {'*       SNR Vs test No'}
    {'Table data size: 25 x 3'}
Block: 12
    {'*       Indoor3'       }
    {'*       SNR Vs test No'}
    {'Table data size: 21 x 18'}
Block: 13
    {'*       Outdoor1'      }
    {'*       SNR Vs test No'}
    {'Table data size: 20 x 18'}
Block: 14
    {'*       Outdoor2'      }
    {'*       SNR Vs test No'}
    {'Table data size: 23 x 3'}
Block: 15
    {'*       Outdoor3'      }
    {'*       SNR Vs test No'}
    {'Table data size: 22 x 18'}
Block: 16
    {'*       Outdoor4'      }
    {'*       SNR Vs test No'}
    {'Table data size: 21 x 18'}
Block: 17
    {'*       Outdoor5'      }
    {'*       SNR Vs test No'}
    {'Table data size: 18 x 5'}

Close Text File


Total Number of Blocks

Determine the number of blocks in the file.

NumBlocks = Block-1
NumBlocks = 17

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;
    {'*       Indoor0'       }
    {'*       SNR Vs test No'}
fprintf('SNR        %d        %d\n',Data{Block,1}(1,2:end))
SNR        -7        -6
   9.0600e-07   6.7100e-07
   3.1700e-07   3.5400e-07
   2.8600e-07   1.9600e-07
   1.4800e-07   7.3400e-07
   3.9500e-08   9.6600e-07
   7.9600e-07   7.8300e-07
   4.0000e-07   8.8100e-07
   3.0100e-07   2.9700e-07

Restore the original Command Window output display format.

set(0, 'format', user_format);

See Also

Related Topics