matlab是fread快还是load快,Matlab中fread的功能 | 学步园

help fread

FREAD  Read binary data from file.

A = FREAD(FID) reads binary data from the specified file

and writes it into matrix A.  FID is an integer file

identifier obtained from FOPEN.  MATLAB reads the entire

file and positions the file pointer at the end of the file

(see FEOF for details).

A = FREAD(FID,SIZE) reads the number of elements specified

by SIZE.  Valid entries for SIZE 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.

A = FREAD(FID,SIZE,PRECISION) reads the file according to

the data format specified by the string PRECISION.  The

PRECISION input commonly contains a datatype specifier like

'int' or 'float', followed by an integer giving the size in

bits.  The SIZE argument is optional when using this syntax.

Any of the following strings, either the MATLAB version, or

their C or Fortran equivalent, may be used.  If not specified,

the default precision 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.

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. 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 = 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.  The SIZE argument is optional when using this syntax.

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.

A = FREAD(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT) treats the data

read as having a format given by the string MACHINEFORMAT. You

can obtain the MACHINEFORMAT argument from the output of the

FOPEN function.   The SIZE and SKIP arguments are optional when

using this syntax.

MACHINEFORMAT is one of the following strings:

Full Precision Support:

'native'      or 'n' - local machine format - the default

'ieee-le'     or 'l' - IEEE floating point with little-endian

byte ordering

'ieee-be'     or 'b' - IEEE floating point with big-endian

byte ordering

'vaxd'        or 'd' - VAX D floating point and VAX ordering

'vaxg'        or 'g' - VAX G floating point and VAX ordering

'ieee-le.l64' or 'a' - IEEE floating point with little-endian

byte ordering and 64 bit long data type

'ieee-be.l64' or 's' - IEEE floating point with big-endian byte

ordering and 64 bit long data type.

Limited Precision Support: (double or equivalent)

'cray'        or 'c' - Cray floating point with big-endian

byte ordering

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

[A, COUNT] = FREAD(...) Optional output argument COUNT returns

the number of elements successfully read.

Examples:

The file alphabet.txt contains the 26 letters of the English

alphabet, all capitalized. Open the file for read access with

fopen, and read the first five elements into output c. Because

a precision has not been specified, MATLAB uses the default

precision of uchar, and the output is numeric:

fid = fopen('alphabet.txt', 'r');

c = fread(fid, 5)'

c =

65    66    67    68    69

fclose(fid);

This time, specify that you want each element read as an unsigned

8-bit integer and output as a character. (Using a precision of

'char=>char' or '*char' will produce the same result):

fid = fopen('alphabet.txt', 'r');

c = fread(fid, 5, 'uint8=>char')'

c =

ABCDE

fclose(fid);

See also fwrite, fseek, fscanf, fgetl, fgets, load, fopen, feof.

Overloaded functions or methods (ones with the same name in other directories)

help serial/fread.m

help udp/fread.m

help icinterface/fread.m

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值