Documentation Center

  • Trials
  • Product Updates

Reading Arbitrary Format Text Files with TEXTSCAN

This example shows how to read an arbitrary format text file with textscan. This function is similar to textread, however it also allows you to read the file one block at a time, and each block can have a different format. The information in the text file test80211.txt is the result from a wireless network communication quality test. Each block is a different environment (e.g., mobile, indoor, outdoor). The numerical results show the data error rate over a range of noise levels for a number of independent tests.

File Format

After 4 lines of introduction, this particular file is made up of a number blocks of data, each with the following format:

  • Two headerlines of description

  • A parameter m

  • A p x m table of data

All the information is read into cell arrays, allowing the storage of different size blocks.

Open the Text File for Reading

fid = fopen('test80211.txt','r');  % Open text file

Read Introduction Lines

InputText=textscan(fid,'%s',4,'delimiter','\n');  % Read strings delimited
                                                  % by a carriage return
Intro=InputText{1};
disp(Intro);
    '*CCX'
    '*CCX WiFi conformance test'
    '*CCX BER Results'
    '*CCX'

Read Each Block

For each block, we read a header, a table name, column headers for the data, then the data itself.

Block = 1;                                         % Initialize block index
while (~feof(fid))                                   % For each block:

    sprintf('Block: %s', num2str(Block));            % Display block number
    InputText=textscan(fid,'%s',2,'delimiter','\n'); % Read header line
    HeaderLines{Block,1}=InputText{1};
    disp(HeaderLines{Block});

    InputText=textscan(fid,'Num SNR=%f');            % Read parameter value
    NumCols=InputText{1};

    FormatString=repmat('%f',1,NumCols);             % Create format string
                                                     % based on parameter
    InputText=textscan(fid,FormatString, ...         % Read data block
                       'delimiter',',');

    Data{Block,1}=cell2mat(InputText);               % Convert to numerical
                                                     % array from cell
    [NumRows,NumCols]=size(Data{Block});             % Size of table
    disp(cellstr([xlate('Table data size: '), ...
                  num2str(NumRows) ' x ' num2str(NumCols)]));
    disp(' ');                                       % New line

    eob=textscan(fid,'%s',1,'delimiter','\n');       % Read and discard EOB
                                                     % marker (Here, 'EOF')
    Block = Block+1;                               % Increment block index
end
    '*       Mobile1'
    '*       SNR Vs test No'

    'Table data size: 30 x 19'

 
    '*       Mobile2'
    '*       SNR Vs test No'

    'Table data size: 30 x 9'

 
    '*       Mobile3'
    '*       SNR Vs test No'

    'Table data size: 31 x 15'

 
    '*       Mobile4'
    '*       SNR Vs test No'

    'Table data size: 28 x 19'

 
    '*       Mobile5'
    '*       SNR Vs test No'

    'Table data size: 32 x 18'

 
    '*       Mobile6'
    '*       SNR Vs test No'

    'Table data size: 30 x 19'

 
    '*       Mobile7'
    '*       SNR Vs test No'

    'Table data size: 30 x 11'

 
    '*       Mobile8'
    '*       SNR Vs test No'

    'Table data size: 20 x 18'

 
    '*       Indoor0'
    '*       SNR Vs test No'

    'Table data size: 9 x 3'

 
    '*       Indoor1'
    '*       SNR Vs test No'

    'Table data size: 22 x 6'

 
    '*       Indoor2'
    '*       SNR Vs test No'

    'Table data size: 25 x 3'

 
    '*       Indoor3'
    '*       SNR Vs test No'

    'Table data size: 21 x 18'

 
    '*       Outdoor1'
    '*       SNR Vs test No'

    'Table data size: 20 x 18'

 
    '*       Outdoor2'
    '*       SNR Vs test No'

    'Table data size: 23 x 3'

 
    '*       Outdoor3'
    '*       SNR Vs test No'

    'Table data size: 22 x 18'

 
    '*       Outdoor4'
    '*       SNR Vs test No'

    'Table data size: 21 x 18'

 
    '*       Outdoor5'
    '*       SNR Vs test No'

    'Table data size: 18 x 5'

 

Close the Text File

fclose(fid);

How Many Blocks

How many blocks were there?

NrOfBlocks = Block-1
NrOfBlocks =

    17

Look at Data

Let's take a look at the numerical data in one of the blocks.

Block=9;

% Headers and Data
disp(HeaderLines{Block});
disp(sprintf('SNR        %d        %d',Data{Block,1}(1,2:end)))

user_format = get(0, 'format');
format short e % Use exponential format

disp(' ');
disp(Data{Block,1}(2:end,2:end));
set(0, 'format', user_format);
    '*       Indoor0'
    '*       SNR Vs test No'

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

Was this topic helpful?