声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 6345|回复: 1

[编程技巧] 用matlab读取二进制文件和ASCII文件的区别

[复制链接]
发表于 2006-11-22 09:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
用matlab读取二进制文件和ASCII文件时,有什么不一样吗?有相关介绍的资料吗?谢谢!

[ 本帖最后由 yejet 于 2006-11-23 11:09 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-11-22 17:46 | 显示全部楼层
>> help fread

FREAD  Read binary data from file.
    [A, COUNT] = FREAD(FID,SIZE,PRECISION) reads binary data from the
    specified file and writes it into matrix A.  Optional output
    argument COUNT returns the number of elements successfully read.
   
    FID is an integer file identifier obtained from FOPEN.
   
    The SIZE argument is optional; if not specified, the entire
    file is read and the file pointer is at the end of the file (see
    FEOF for details); if specified, valid entries are:
        N      read N elements into a column vector.
        inf    read to the end of the file.
        [M,N]  read elements to fill an M-by-N matrix, in column order.
               N can be inf, but M can't.
   
    The PRECISION argument is a string that specifies the format
    of the data to be read. It commonly contains a datatype specifier
    like 'int' or 'float' followed by an integer giving the size in
    bits.  Any of the following strings, either the MATLAB version,
    or their C or Fortran equivalent, may be used.  If not specified,
    the default is 'uchar'.
   
        MATLAB    C or Fortran     Description
        'uchar'   'unsigned char'  unsigned character,  8 bits.
        'schar'   'signed char'    signed character,  8 bits.
        'int8'    'integer*1'      integer, 8 bits.
        'int16'   'integer*2'      integer, 16 bits.
        'int32'   'integer*4'      integer, 32 bits.
        'int64'   'integer*8'      integer, 64 bits.
        'uint8'   'integer*1'      unsigned integer, 8 bits.
        'uint16'  'integer*2'      unsigned integer, 16 bits.
        'uint32'  'integer*4'      unsigned integer, 32 bits.
        'uint64'  'integer*8'      unsigned integer, 64 bits.
        'single'  'real*4'         floating point, 32 bits.
        'float32' 'real*4'         floating point, 32 bits.
        'double'  'real*8'         floating point, 64 bits.
        'float64' 'real*8'         floating point, 64 bits.

    For example,

        type fread.m

    displays the complete M-file containing this FREAD help entry.  To
    simulate this command one could enter instead:

        fid = fopen('fread.m','r');
        F = fread(fid);
        s = char(F')

    The FOPEN command opens the M-file on the MATLAB path with name,
    'fread.m' for reading. The FREAD command assumes the default SIZE of
    inf and the default PRECISION of 'uchar'. It reads the entire file
    converting the unsigned characters into a column vector of class
    'double' (double precision floating point).  To display the result
    as readable text the 'double' column vector is transposed to a
    row vector and converted to class 'char' (character) using the CHAR
    function.

    The following platform dependent formats are also supported but
    they are not guaranteed to be the same size on all platforms.

        MATLAB    C or Fortran     Description
        'char'    'char*1'         character,  8 bits (signed or unsigned).
        'short'   'short'          integer,  16 bits.
        'int'     'int'            integer,  32 bits.
        'long'    'long'           integer,  32 or 64 bits.
        'ushort'  'unsigned short' unsigned integer,  16 bits.
        'uint'    'unsigned int'   unsigned integer,  32 bits.
        'ulong'   'unsigned long'  unsigned integer,  32 bits or 64 bits.
        'float'   'float'          floating point, 32 bits.

    The following formats map to an input stream of bits rather than
    bytes.

        'bitN'                     signed integer, N bits  (1<=N<=64).
        'ubitN'                    unsigned integer, N bits (1<=N<=64).

    If the input stream is bytes and FREAD reaches the end of file
    (see FEOF) in the middle of reading the number of bytes required
    for an element, the partial result is ignored. However, if the
    input stream is bits, then the partial result is returned as the
    last value.  If an error occurs before reaching the end of file,
    only full elements read up to that point are used.

    By default, numeric values are returned in class 'double' arrays.
    To return numeric values stored in classes other than double,
    create your PRECISION argument by first specifying your source
    format, then following it by '=>', and finally specifying your
    destination format. You are not required to use the exact name
    of a MATLAB class type (see CLASS for details) for the destination.
    The name is translated for you to the name of the most appropriate
    MATLAB class type.  If the source and destination formats are the
    same then the following shorthand notation may be used:

        *source

    which means:

        source=>source

    For example,

        uint8=>uint8               read in unsigned 8-bit integers and
                                   save them in an unsigned 8-bit integer
                                   array

        *uint8                     shorthand version of previous example

        bit4=>int8                 read in signed 4-bit integers packed
                                   in bytes and save them in a signed
                                   8-bit integer array (each 4-bit
                                   integer becomes one 8-bit integer)

        double=>real*4             read in doubles, convert and save
                                   as a 32-bit floating point array

    [A, COUNT] = FREAD(FID,SIZE,PRECISION,SKIP) includes a SKIP argument
    that specifies the number of bytes to skip after each PRECISION value
    is read. If PRECISION specifies a bit source format, like 'bitN' or
    'ubitN', the SKIP argument is interpreted as the number of bits to
    skip.

    When SKIP is used, the PRECISION string may contain a positive
    integer repetition factor of the form 'N*' which prepends the source
    format of the PRECISION argument, like '40*uchar'.  Note that 40*uchar
    for the PRECISION alone is equivalent to '40*uchar=>double', not
    '40*uchar=>uchar'.  With SKIP specified, FREAD reads in, at most, a
    repetition factor number of values (default of 1), does a skip of
    input specified by the SKIP argument, reads in another block of values
    and does a skip of input, etc. until SIZE number of values have been
    read.  If a SKIP argument is not specified, the repetition factor is
    ignored.  Repetition with skip is useful for extracting data in
    noncontiguous fields from fixed length records.

    For example,

        s = fread(fid,120,'40*uchar=>uchar',8);

    reads in 120 characters in blocks of 40 each separated by 8 characters.
    Note that the class type of s is 'uint8' since it is the appropriate
    class corresponding to the destination format, 'uchar'.  Also since 40
    evenly divides 120 the last block read is a full block which means
    that a final skip will be done before the command is finished.  If the
    last block read is not a full block then FREAD will not finish with a
    skip.

    See FOPEN on how to read Big and Little Endian files.

    See also FWRITE, FSEEK, FSCANF, FGETL, FGETS, LOAD, FOPEN, FEOF.

Overloaded methods
    help serial/fread.m
    help udp/fread.m
    help instrument/fread.m

>> help textread

TEXTREAD Read formatted data from text file.
     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.

     [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 DLMREAD, LOAD, STRREAD, SSCANF, XLSREAD.
你自己比较比较,看看他们有什么不同。

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-14 10:08 , Processed in 0.069095 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表