Mapping Toolbox | ![]() ![]() |
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 will match 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 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 which 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 record which match, starting the search in column 11.
Limitations
Searches are limited to field containing character data.
Remarks
See readfields
for a complete discussion of the format and contents of the fields
argument.
See Also
readfields |
Read fields or records from a fixed format file |
![]() | gradientm | gridm | ![]() |