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.
After 4 lines of introduction, this particular file is made up of a number blocks of data, each with the following format:
All the information is read into cell arrays, allowing the storage of different size blocks.
fid = fopen('test80211.txt','r'); % Open text file
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'
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... disp(['Block: ' 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,'delimiter',','); % Read data block Data{Block,1}=cell2mat(InputText); % Convert to numerical array from cell [NumRows,NumCols]=size(Data{Block}); % Size of table disp(cellstr(['Table data size: ' num2str(NumRows) ' x ' num2str(NumCols)])); disp(' '); % New line eob=textscan(fid,'%s',1,'delimiter','\n'); % Read and discard EOB marker ('EOF' in this case) Block = Block+1; % Increment block index end
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'
fclose(fid);
How many blocks were there?
NrOfBlocks = Block-1
NrOfBlocks =
17
Let's take a look at the numerical data in one of the blocks.
Block=9; % Headers and Data disp(HeaderLines{Block}); disp(['SNR' sprintf(' %d',Data{Block,1}(1,2:end))]) format short e % Use exponential format disp(' '); disp(Data{Block,1}(2:end,2:end));
'* Indoor0'
'* SNR Vs test No'
SNR -7 -6
9.0600e-007 6.7100e-007
3.1700e-007 3.5400e-007
2.8600e-007 1.9600e-007
1.4800e-007 7.3400e-007
3.9500e-008 9.6600e-007
7.9600e-007 7.8300e-007
4.0000e-007 8.8100e-007
3.0100e-007 2.9700e-007