Products & Services Solutions Academia Support User Community Company

Learn more about Mapping Toolbox   

grepfields - Identify matching fields in fixed record length files

Syntax

grepfields(filename,searchstring)
grepfields(filename,searchstring,casesens)
grepfields(filename,searchstring,casesens,startcol)
grepfields(filename,searchstring,casesens,startfield,fields)
grepfields(filename,searchstring,casesens,startfield,fields, machineformat)
indx = grepfields(...)

Description

grepfields(filename,searchstring) displays lines in the file that begin with the search string. The file must have fixed-length records with line endings.

grepfields(filename,searchstring,casesens), with casesens 'matchcase', specifies a case-sensitive search. If omitted or 'none', the search string matches regardless of the case.

grepfields(filename,searchstring,casesens,startcol) searches starting with the specified column. startcol is an integer between 1 and the bytes per record in the file. In this calling form, the file is regarded as a text file with line endings.

grepfields(filename,searchstring,casesens,startfield,fields) searches within the specified field. startfield is an integer between 1 and the number of fields per record. The format of the file is described by the fields structure. See readfields for recognized fields structure entries. In this calling form, the file can be binary and lack line endings. The search is within startfield, which must be a character field.

grepfields(filename,searchstring,casesens,startfield,fields, machineformat) opens the file with the specified machine format. machineformat must be recognized by fopen.

indx = grepfields(...) returns the record numbers of matched records instead of displaying them on screen.

Example

Write a binary file and read it:

fid = fopen('testbin','wb');
for i = 1:3	
	fwrite(fid,['character' num2str(i) ],'char');
	fwrite(fid,i,'int8');
	fwrite(fid,[i i],'int16');
	fwrite(fid,i,'integer*4'); 
	fwrite(fid,i,'real*8');
end
fclose(fid);

fs(1).length = 10;fs(1).type = 'char';fs(1).name = 'field 1';
fs(2).length = 1;fs(2).type = 'int8';fs(2).name = 'field 2';
fs(3).length = 2;fs(3).type = 'int16';fs(3).name = 'field 3';
fs(4).length = 1;fs(4).type = 'integer*4';fs(4).name = 'field 4'; 
fs(5).length = 1;fs(5).type = 'float64';fs(5).name = 'field 5';

Find the record matching the string 'character2'. The record contains binary data, which cannot be properly displayed.

grepfields('testbin','character2','none',1,fs)
character2? ? ?   ?@ 

indx = grepfields('testbin','character2','none',1,fs)
indx =
     2

Read the formatted file containing the following:

--------------------------------------------------------
character data 1  1  2  3 1e6 10D6

character data 2 11 22 33 2e6 20D6

character data 3111222333 3e6 30D6
--------------------------------------------------------
fs(1).length = 16;fs(1).type = 'char';fs(1).name = 'field 1';
fs(2).length = 3;fs(2).type = '%3d';fs(2).name = 'field 2';
fs(3).length = 1;fs(3).type = '%4g';fs(3).name = 'field 3';
fs(4).length = 1;fs(4).type = '%5D';	fs(4).name = 'field 4';
fs(5).length = 1;fs(5).type = 'char';fs(5).name = '';

Find the records that match at the beginning of the line.

grepfields('testfile1','character')
character data 1  1  2  3 1e6 10D6
character data 2 11 22 33 2e6 20D6
character data 3111222333 3e6 30D6

grepfields('testfile1','character data 2')
character data 2 11 22 33 2e6 20D6

Find the records that match, starting the search in column 11.

grepfields('testfile1','data 2','none',11)
character data 2 11 22 33 2e6 20D6

Search record number 1.

grepfields('testfile1','character data 2','none',1,fs)
character data 2 11 22 33 2e6 20D6

Limitations

Searches are limited to fields containing character data.

Remarks

See readfields for a complete discussion of the format and contents of the fields argument.

See Also

readfields, fopen

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS