Home > other > ctextread.m

ctextread

PURPOSE ^

CTEXTREAD Read formatted data from text file. Modified from Matlab

SYNOPSIS ^

function varargout = ctextread(varargin);

DESCRIPTION ^

CTEXTREAD Read formatted data from text file. Modified from Matlab
          TEXTREAD to avoid use of WHICH()
    A = TEXTREAD('FILENAME')
    A = TEXTREAD('FILENAME','',N)
    A = TEXTREAD('FILENAME','',param,value, ...)
    A = TEXTREAD('FILENAME','',N,param,value, ...) reads numeric data from
    the file FILENAME into a single variable.  If the file contains any
    text data, an error is produced. FILENAME should include a
    correct path, either relative to the current directory or
    fully qualified. 

    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT')
    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N)
    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',param,value, ...)
    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N,param,value, ...) reads
    data from the file FILENAME into the variables A,B,C,etc.  The type of
    each return argument is given by the FORMAT string.  The number of
    return arguments must match the number of conversion specifiers in the
    FORMAT string.  If there are fewer fields in the file than in the
    format string, an error is produced.  See FORMAT STRINGS below for
    more information.

    If N is specified, the format string is reused N times.  If N is -1 (or
    not specified) TEXTREAD reads the entire file.

    If param,value pairs are supplied, user configurable options customize
    the behavior of TEXTREAD.  See USER CONFIGURABLE OPTIONS below.

    TEXTREAD works by matching and converting groups of characters from the
    file. An input field is defined as a string of non-whitespace
    characters extending to the next whitespace or delimiter character
    or until the field width is exhausted.  Repeated delimiter characters
    are significant while repeated whitespace characters are treated as
    one.

    FORMAT STRINGS

    If the FORMAT string is empty, TEXTREAD will only numeric data.

    The FORMAT string can contain whitespace characters (which are
    ignored), ordinary characters (which are expected to match the next
    non-whitespace character in the input), or conversion specifications.

    If whitespace is set to '' and format types are %s,%q,%[...] and %[^...]. 
    Else whitespace characters are ignored.

    Supported conversion specifications:
        %n - read a number - float or integer (returns double array)
             %5n reads up to 5 digits or until next delimiter
        %d - read a signed integer value (returns double array)
             %5d reads up to 5 digits or until next delimiter
        %u - read an integer value (returns double array)
             %5u reads up to 5 digits or until next delimiter
        %f - read a floating point value (returns double array)
             %5f reads up to 5 digits or until next delimiter
        %s - read a whitespace separated string (returns cellstr)
             %5s reads up to 5 characters or until whitespace
        %q - read a (possibly double quoted) string (returns cellstr)
             %5q reads up to 5 non-quote characters or until whitespace
        %c - read character or whitespace (returns char array)
             %5c reads up to 5 characters including whitespace
        %[...]  - reads characters matching characters between the
                  brackets until first non-matching character or
                  whitespace (returns cellstr)
                  use %[]...] to include ]
             %5[...] reads up to 5 characters
        %[^...] - reads characters not matching characters between the
                  brackets until first matching character or whitespace
                  (returns cellstr)
                  use %[^]...] to exclude ]
             %5[^...] reads up to 5 characters

    Note: Format strings are interpreted as with sprintf before parsing.
    For example, textread('mydata.dat','%s\t') will search for a tab not
    the character '\' followed by the character 't'.  See the Language
    Reference Guide or a C manual for complete details.

    Using %* instead of % in a conversion causes TEXTREAD to skip the
    matching characters in the input (and no output is created for this
    conversion).  The % can be followed by an optional field width to
    handle fixed width fields. For example %5d reads a 5 digit integer. In
    addition the %f format supports the form %<width>.<prec>f.

    USER CONFIGURABLE OPTIONS

    Possible param/value options are:
         'bufsize'      - maximum string length in bytes (default is 4095)
         'commentstyle' - one of 
              'matlab'  -- characters after % are ignored
              'shell'   -- characters after # are ignored
              'c'       -- characters between /* and */ are ignored
              'c++'    -- characters after // are ignored
         'delimiter'    - delimiter characters (default is none)
         'emptyvalue'   - empty cell value in delimited files (default is 0)
         'endofline'    - end of line character (default determined from file)
         'expchars'     - exponent characters (default is 'eEdD')
         'headerlines'  - number of lines at beginning of file to skip
         'whitespace'   - whitespace characters (default is ' \b\t')
    
    TEXTREAD is useful for reading text files with a known format.  Both
    fixed and free format files can be handled.

    Examples:
     Suppose the text file mydata.dat contains data in the following form:
        Sally    Type1 12.34 45 Yes
        Joe      Type2 23.54 60 No
        Bill     Type1 34.90 12 No
          
     Read each column into a variable
       [names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');

     Read first column into a cell array (skipping rest of line)
       [names]=textread('mydata.dat','%s%*[^\n]')

     Read first character into char array (skipping rest of line)
       [initials]=textread('mydata.dat','%c%*[^\n]')

     Read file as a fixed format file while skipping the doubles
       [names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');

     Read file and match Type literal
       [names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');

     Read m-file into cell array of strings
       file = textread('fft.m','%s','delimiter','\n','whitespace','');

     To read all numeric data from a delimited text file, use a single output
     argument, empty format string, and the appropriate delimiter. For 
     example, suppose data.csv contains:
       1,2,3,4
       5,6,7,8
       9,10,11,12

     Read the whole matrix into a single variable:
       [data] = textread('data.csv','','delimiter',',');

     Read the first two columns into two variables:
       [col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');

     For files with empty cells, use the emptyvalue parameter.  Suppose
     data.csv contains:
       1,2,3,4,,6
       7,8,9,,11,12

     Read the file like this, using NaN in empty cells:
       [data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);

     
   See also TEXTREAD, DLMREAD, LOAD, STRREAD, SSCANF, XLSREAD.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = ctextread(varargin);
0002 %CTEXTREAD Read formatted data from text file. Modified from Matlab
0003 %          TEXTREAD to avoid use of WHICH()
0004 %    A = TEXTREAD('FILENAME')
0005 %    A = TEXTREAD('FILENAME','',N)
0006 %    A = TEXTREAD('FILENAME','',param,value, ...)
0007 %    A = TEXTREAD('FILENAME','',N,param,value, ...) reads numeric data from
0008 %    the file FILENAME into a single variable.  If the file contains any
0009 %    text data, an error is produced. FILENAME should include a
0010 %    correct path, either relative to the current directory or
0011 %    fully qualified.
0012 %
0013 %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT')
0014 %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N)
0015 %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',param,value, ...)
0016 %    [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N,param,value, ...) reads
0017 %    data from the file FILENAME into the variables A,B,C,etc.  The type of
0018 %    each return argument is given by the FORMAT string.  The number of
0019 %    return arguments must match the number of conversion specifiers in the
0020 %    FORMAT string.  If there are fewer fields in the file than in the
0021 %    format string, an error is produced.  See FORMAT STRINGS below for
0022 %    more information.
0023 %
0024 %    If N is specified, the format string is reused N times.  If N is -1 (or
0025 %    not specified) TEXTREAD reads the entire file.
0026 %
0027 %    If param,value pairs are supplied, user configurable options customize
0028 %    the behavior of TEXTREAD.  See USER CONFIGURABLE OPTIONS below.
0029 %
0030 %    TEXTREAD works by matching and converting groups of characters from the
0031 %    file. An input field is defined as a string of non-whitespace
0032 %    characters extending to the next whitespace or delimiter character
0033 %    or until the field width is exhausted.  Repeated delimiter characters
0034 %    are significant while repeated whitespace characters are treated as
0035 %    one.
0036 %
0037 %    FORMAT STRINGS
0038 %
0039 %    If the FORMAT string is empty, TEXTREAD will only numeric data.
0040 %
0041 %    The FORMAT string can contain whitespace characters (which are
0042 %    ignored), ordinary characters (which are expected to match the next
0043 %    non-whitespace character in the input), or conversion specifications.
0044 %
0045 %    If whitespace is set to '' and format types are %s,%q,%[...] and %[^...].
0046 %    Else whitespace characters are ignored.
0047 %
0048 %    Supported conversion specifications:
0049 %        %n - read a number - float or integer (returns double array)
0050 %             %5n reads up to 5 digits or until next delimiter
0051 %        %d - read a signed integer value (returns double array)
0052 %             %5d reads up to 5 digits or until next delimiter
0053 %        %u - read an integer value (returns double array)
0054 %             %5u reads up to 5 digits or until next delimiter
0055 %        %f - read a floating point value (returns double array)
0056 %             %5f reads up to 5 digits or until next delimiter
0057 %        %s - read a whitespace separated string (returns cellstr)
0058 %             %5s reads up to 5 characters or until whitespace
0059 %        %q - read a (possibly double quoted) string (returns cellstr)
0060 %             %5q reads up to 5 non-quote characters or until whitespace
0061 %        %c - read character or whitespace (returns char array)
0062 %             %5c reads up to 5 characters including whitespace
0063 %        %[...]  - reads characters matching characters between the
0064 %                  brackets until first non-matching character or
0065 %                  whitespace (returns cellstr)
0066 %                  use %[]...] to include ]
0067 %             %5[...] reads up to 5 characters
0068 %        %[^...] - reads characters not matching characters between the
0069 %                  brackets until first matching character or whitespace
0070 %                  (returns cellstr)
0071 %                  use %[^]...] to exclude ]
0072 %             %5[^...] reads up to 5 characters
0073 %
0074 %    Note: Format strings are interpreted as with sprintf before parsing.
0075 %    For example, textread('mydata.dat','%s\t') will search for a tab not
0076 %    the character '\' followed by the character 't'.  See the Language
0077 %    Reference Guide or a C manual for complete details.
0078 %
0079 %    Using %* instead of % in a conversion causes TEXTREAD to skip the
0080 %    matching characters in the input (and no output is created for this
0081 %    conversion).  The % can be followed by an optional field width to
0082 %    handle fixed width fields. For example %5d reads a 5 digit integer. In
0083 %    addition the %f format supports the form %<width>.<prec>f.
0084 %
0085 %    USER CONFIGURABLE OPTIONS
0086 %
0087 %    Possible param/value options are:
0088 %         'bufsize'      - maximum string length in bytes (default is 4095)
0089 %         'commentstyle' - one of
0090 %              'matlab'  -- characters after % are ignored
0091 %              'shell'   -- characters after # are ignored
0092 %              'c'       -- characters between /* and */ are ignored
0093 %              'c++'    -- characters after // are ignored
0094 %         'delimiter'    - delimiter characters (default is none)
0095 %         'emptyvalue'   - empty cell value in delimited files (default is 0)
0096 %         'endofline'    - end of line character (default determined from file)
0097 %         'expchars'     - exponent characters (default is 'eEdD')
0098 %         'headerlines'  - number of lines at beginning of file to skip
0099 %         'whitespace'   - whitespace characters (default is ' \b\t')
0100 %
0101 %    TEXTREAD is useful for reading text files with a known format.  Both
0102 %    fixed and free format files can be handled.
0103 %
0104 %    Examples:
0105 %     Suppose the text file mydata.dat contains data in the following form:
0106 %        Sally    Type1 12.34 45 Yes
0107 %        Joe      Type2 23.54 60 No
0108 %        Bill     Type1 34.90 12 No
0109 %
0110 %     Read each column into a variable
0111 %       [names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
0112 %
0113 %     Read first column into a cell array (skipping rest of line)
0114 %       [names]=textread('mydata.dat','%s%*[^\n]')
0115 %
0116 %     Read first character into char array (skipping rest of line)
0117 %       [initials]=textread('mydata.dat','%c%*[^\n]')
0118 %
0119 %     Read file as a fixed format file while skipping the doubles
0120 %       [names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');
0121 %
0122 %     Read file and match Type literal
0123 %       [names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');
0124 %
0125 %     Read m-file into cell array of strings
0126 %       file = textread('fft.m','%s','delimiter','\n','whitespace','');
0127 %
0128 %     To read all numeric data from a delimited text file, use a single output
0129 %     argument, empty format string, and the appropriate delimiter. For
0130 %     example, suppose data.csv contains:
0131 %       1,2,3,4
0132 %       5,6,7,8
0133 %       9,10,11,12
0134 %
0135 %     Read the whole matrix into a single variable:
0136 %       [data] = textread('data.csv','','delimiter',',');
0137 %
0138 %     Read the first two columns into two variables:
0139 %       [col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');
0140 %
0141 %     For files with empty cells, use the emptyvalue parameter.  Suppose
0142 %     data.csv contains:
0143 %       1,2,3,4,,6
0144 %       7,8,9,,11,12
0145 %
0146 %     Read the file like this, using NaN in empty cells:
0147 %       [data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);
0148 %
0149 %
0150 %   See also TEXTREAD, DLMREAD, LOAD, STRREAD, SSCANF, XLSREAD.
0151 
0152 %   Clay M. Thompson 3-3-98
0153 %   Copyright 1984-2002 The MathWorks, Inc.
0154 %   Based on MathWorks Revision: 1.16 $ $Date: 2002/06/05 20:10:18
0155 
0156 % $Id: ctextread.m,v 1.1 2004/05/19 15:03:59 stuver Exp $
0157 
0158 % do some preliminary error checking
0159 error(nargchk(1,inf,nargin));
0160 
0161 % From MathWorks textread: commented out
0162 %if (exist(varargin{1}) ~= 2 | exist(fullfile(cd,varargin{1})) ~= 2) & ~isempty(which(varargin{1}))
0163 %    varargin{1} = which(varargin{1});
0164 %end
0165 
0166 if exist(varargin{1}) ~= 2
0167     error('File not found.');
0168 end
0169 
0170 if nargout == 0
0171     nlhs = 1;
0172 else
0173     nlhs = nargout;
0174 end
0175 
0176 [varargout{1:nlhs}]=dataread('file',varargin{:});
0177 
0178 return

Generated on Tue 05-Oct-2004 10:40:50 by m2html © 2003