Fread Not Reading Proper Float Vlaue C
Read binary data from file
Syntax
-
A = fread(fid) A = fread(fid, count) A = fread(fid, count, precision) A = fread(fid, count, precision, skip) A = fread(fid, count, precision, skip, machineformat) [A, count] = fread(...)
Description
A = fread(fid)
reads data in binary format from the file specified by fid
into matrix A
. Open the file using fopen
before calling fread
. The fid
argument is the integer file identifier obtained from the fopen
performance. MATLAB reads the file from starting time to end, and and so positions the file arrow at the end of the file (meet feof
for details).
Note fread
is intended primarily for binary data. When reading text files, use the fgetl
function.
A = fread(fid, count)
reads the number of elements specified by count. At the end of the fread
, MATLAB sets the file pointer to the next byte to be read. A subsequent fread
will begin at the location of the file pointer. Run into Specifying the Number of Elements, below.
Note In the following syntaxes, the count and skip
arguments are optional. For example, fread(fid, precision)
is a valid syntax.
A = fread(fid, count, precision)
reads the file according to the data format specified by the string precision
. This statement commonly contains a data blazon specifier such as int
or float
, followed by an integer giving the size in bits. Encounter Specifying Precision and Specifying Output Precision, below.
A = fread(fid, count, precision, skip)
includes an optional skip
argument that specifies the number of bytes to skip after each precision
value is read. If precision
specifies a bit format like 'bitN'
or 'ubitN'
, the skip
argument is interpreted every bit the number of bits to skip. Come across Specifying a Skip Value, below.
A = fread(fid, count, precision, skip, machineformat)
treats the data read as having a format given past machineformat
. You can obtain the machineformat
statement from the output of the fopen
function. See Specifying Machine Format, beneath.
[A, count] = fread(...)
returns the data read from the file in A
, and the number of elements successfully read in count
.
Specifying the Number of Elements
Valid options for count are
n | Reads n elements into a column vector. |
inf | Reads to the finish of the file, resulting in a column vector containing the aforementioned number of elements as are in the file. If using inf results in an "out of memory" error, specify a numeric count value. |
[one thousand,due north] | Reads enough elements to make full an m -by-n matrix, filling in elements in column social club, padding with zeros if the file is besides small to fill up the matrix. n can be specified as inf , but m cannot. |
Specifying Precision
Any of the strings in the following table, either the MATLAB version or their C or Fortran equivalent, can be used for precision
. If precision
is not specified, MATLAB uses the default, which is 'uchar'
.
MATLAB | C or Fortran | Interpretation |
'schar' | 'signed char' | Signed character; eight bits |
'uchar' | 'unsigned char' | Unsigned character; 8 bits |
'int8' | 'integer*1' | Integer; 8 $.25 |
'int16' | 'integer*ii' | Integer; 16 bits |
'int32' | 'integer*4' | Integer; 32 $.25 |
'int64' | 'integer*8' | Integer; 64 bits |
'uint8' | 'integer*1' | Unsigned integer; eight $.25 |
'uint16' | 'integer*2' | Unsigned integer; xvi bits |
'uint32' | 'integer*4' | Unsigned integer; 32 bits |
'uint64' | 'integer*eight' | Unsigned integer; 64 bits |
'float32' | 'existent*4' | Floating-point; 32 $.25 |
'float64' | 'real*8' | Floating-indicate; 64 bits |
'double' | 'real*viii' | Floating-point; 64 bits |
The following platform-dependent formats are too supported, simply they are not guaranteed to be the same size on all platforms.
MATLAB | C or Fortran | Interpretation |
'char' | 'char*i' | Graphic symbol; 8 bits |
'brusque' | 'curt' | Integer; sixteen $.25 |
'int' | 'int' | Integer; 32 $.25 |
'long' | 'long' | Integer; 32 or 64 bits |
'ushort' | 'unsigned short' | Unsigned integer; xvi bits |
'uint' | 'unsigned int' | Unsigned integer; 32 $.25 |
'ulong' | 'unsigned long' | Unsigned integer; 32 or 64 $.25 |
'float' | 'bladder' | Floating-point; 32 bits |
The post-obit formats map to an input stream of bits rather than bytes.
Specifying Output Precision
By default, numeric values are returned in form double
arrays. To return numeric values stored in classes other than double
, create your precision argument by commencement specifying your source format, and so following it with the characters "=>
", and finally specifying your destination format. You are not required to use the verbal name of a MATLAB class type for destination. (See form
for details). fread
translates the proper noun to the most appropriate MATLAB form blazon. If the source and destination formats are the aforementioned, the post-obit shorthand notation tin exist used.
-
*source
which means
-
source=>source
For example, '*uint16'
is the same equally 'uint16=>uint16'
.
This table shows some instance precision format strings.
'uint8=>uint8' | Read in unsigned eight-chip integers and save them in an unsigned eight-bit integer array. |
'*uint8' | Shorthand version of the higher up. |
'bit4=>int8' | Read in signed four-bit integers packed in bytes and save them in a signed eight-chip array. Each 4-bit integer becomes an 8-fleck integer. |
'double=>real*4' | Read in doubles, convert, and salvage as a 32-bit floating-point array. |
Specifying a Skip Value
When skip
is used, the precision
string tin can incorporate a positive integer repetition factor of the form 'Northward*'
, which prefixes the source format specification, such as '40*uchar'
.
Note Do not confuse the asterisk (*) used in the repetition factor with the asterisk used as precision format shorthand. The format cord '40*uchar'
is equivalent to 'twoscore*uchar=>double'
, non '40*uchar=>uchar'
.
When skip
is specified, fread
reads in, at well-nigh, a repetition factor number of values (default is one), skips the amount of input specified past the skip
argument, reads in some other block of values, over again skips input, and and so on, until count number of values have been read. If a skip
argument is not specified, the repetition factor is ignored. Use the repetition factor with the skip
argument to extract data in noncontiguous fields from fixed-length records.
Specifying Machine Format
machineformat
is one of the following strings:
'cray' or 'c' | Cray floating bespeak with big-endian byte ordering |
'ieee-be' or 'b' | IEEE floating point with big-endian byte ordering |
'ieee-le' or 'l' | IEEE floating indicate with little-endian byte ordering |
'ieee-be.l64' or 'due south' | IEEE floating indicate with big-endian byte ordering and 64-bit long data type |
'ieee-le.l64' or 'a' | IEEE floating indicate with fiddling-endian byte ordering and 64-flake long data type |
'native' or 'n' | Numeric format of the machine on which MATLAB is running (the default) |
'vaxd' or 'd' | VAX D floating point and VAX ordering |
'vaxg' or 'g' | VAX Chiliad floating point and VAX ordering |
Remarks
When reading graphic symbol strings from files, laissez passer the output of fread
to the MATLAB native2unicode
function to ensure that characters display correctly:
-
str = native2unicode(fread(fid));
If the input stream is bytes and fread
reaches the end of file (see feof
) in the centre of reading the number of bytes required for an chemical element, the partial result is ignored. Yet, if the input stream is bits, so the fractional effect is returned as the concluding value. If an error occurs before reaching the finish of file, only total elements read up to that betoken are used.
Examples
Case one
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 starting time five elements into output c
. Because a precision has non been specified, MATLAB uses the default precision of uchar
, and the output is numeric:
-
fid = fopen('alphabet.txt', 'r'); c = fread(fid, v)' c = 65 66 67 68 69 fclose(fid);
This fourth dimension, specify that you want each element read as an unsigned 8-bit integer and output as a graphic symbol. (Using a precision of 'char=>char'
or '*char'
volition produce the same result):
-
fid = fopen('alphabet.txt', 'r'); c = fread(fid, 5, 'uint8=>char')' c = ABCDE fclose(fid);
When you lot leave out the optional count argument, MATLAB reads the file to the end, A
through Z
:
-
fid = fopen('alphabet.txt', 'r'); c = fread(fid, '*char')' c = ABCDEFGHIJKLMNOPQRSTUVWXYZ fclose(fid);
The fopen
function positions the file pointer at the start of the file. And so the first fread
in this example reads the outset five elements in the file, and so repositions the file arrow at the commencement of the adjacent element. For this reason, the side by side fread
picks upward where the previous fread
left off, at the graphic symbol F
.
-
fid = fopen('alphabet.txt', 'r'); c1 = fread(fid, 5, '*char'); c2 = fread(fid, 8, '*char'); c3 = fread(fid, five, '*char'); fclose(fid); sprintf('%c', c1, ' * ', c2, ' * ', c3) ans = ABCDE * FGHIJKLM * NOPQR
Skip two elements between each read past specifying a skip
argument of 2
:
-
fid = fopen('alphabet.txt', 'r'); c = fread(fid, 'char', 2); % Skip 2 bytes per read fclose(fid); sprintf('%c', c) ans = ADGJMPSVY
Example ii
This control displays the consummate M-file containing this fread
help entry:
-
type fread.yard
To simulate this command using fread
, enter the post-obit:
-
fid = fopen('fread.m', 'r'); F = fread(fid, '*char')'; fclose(fid);
In the example, the fread
command assumes the default size, 'inf'
, and precision '*uchar'
(the same as 'char=>char'
). fread
reads the entire file. To brandish the result equally readable text, the column vector is transposed to a row vector.
Case iii
As some other example,
-
south = fread(fid, 120, '40*uchar=>uchar', 8);
reads in 120 characters in blocks of 40, each separated by 8 characters. Annotation that the course type of south
is 'uint8'
since it is the appropriate grade corresponding to the destination format 'uchar'
. Also, since 40 evenly divides 120, the final block read is a full block, which means that a last skip is done before the command is finished. If the concluding cake read is not a full block, then fread
does not end with a skip.
Encounter fopen
for information about reading big and little-endian files.
Instance 4
Invoke the fopen
function with simply an fid
input statement to obtain the machine format for the file. You can see that this file was written in IEEE floating signal with little-endian byte ordering ('ieee-le'
) format:
-
fid = fopen('A1.dat', 'r'); [fname, mode, mformat] = fopen(fid); mformat mformat = ieee-le
Utilize the MATLAB format function (non related to the machine format type) to have MATLAB display output using hexadecimal:
-
format hex
At present use the machineformat
input with fread
to read the data from the file using the same format:
-
10 = fread(fid, 6, 'uint64', 'ieee-le') ten = 4260800000002000 0000000000000000 4282000000180000 0000000000000000 42ca5e0000258000 42f0000464d45200 fclose(fid);
Modify the machine format to IEEE floating point with large-endian byte ordering ('ieee-be'
) and verify that you get different results:
-
fid = fopen('A1.dat', 'r'); x = fread(fid, half dozen, 'uint64', 'ieee-be') x = 4370000008400000 0000000000000000 4308000200100000 0000000000000000 4352c0002f0d0000 43c022a6a3000000 fclose(fid);
See Also
fgetl
, fscanf
, fwrite
, fprintf
, fopen
, fclose
, fseek
, ftell
, feof
frameedit | freqspace |
© 1994-2005 The MathWorks, Inc.
shapirosanclaus1973.blogspot.com
Source: http://matlab.izmiran.ru/help/techdoc/ref/fread.html
0 Response to "Fread Not Reading Proper Float Vlaue C"
Publicar un comentario