Read 5 answers by scientists to the question asked by Gargi Akhoury on Jul 22, 2014.
I have a data file that uses (char(1 byte), charn(array of n chars), word(2 byte unsigned int), short(2 byte signed int), dword(4 byte unsigned int), long(4 byte signed int) and float(4 byte real)) and is supposedly in the following format. I am reading the data file into MATLAB with fopen, fread, etc.
But the values I am getting are not what I expect. Format:. char8. I would recommend first, just read in all the data at once as a byte array. You'll be able to debug the problem much faster: fid = fopen('MIC1.001','rb'); data = fread(fid); fclose(fid);% could look at it as all chars, just for debugging char(A)' The data is read in as a large array of bytes.
![Matlab Read Binary Data Matlab Read Binary Data](/uploads/1/2/5/4/125428263/351026620.gif)
Then you would go through and parse the bytes by casting them appropriately. Okay when I put it in MATLAB I get the same output as you. Forgive me for not knowing much about this, but I'm still a little lost. I've been trying to read up on the byte swapping as you mentioned but I was under the impression that MATLAB would pick up on whether the data was big or little endian and would then convert it accordingly.
Am I wrong in assuming that? Maybe that is why I am getting random values like 2778916 when my data system manual says it should be a number between 0 and 3.
– Jun 26 '13 at 15:56.
Fread (MATLAB Function Reference) Read binary data from file Syntax A,count = fread(fid,size, precision) A,count = fread(fid,size, precision,skip) Description 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. Size is an optional argument that determines how much data is read. If size is not specified, fread reads to the end of the file. Valid options are: n Reads n elements into a column vector. Inf Reads to the end of the file, resulting in a column vector containing the same number of elements as are in the file.
m,n Reads enough elements to fill an m-by-n matrix, filling in elements in column order, padding with zeros if the file is too small to fill the matrix. If fread reaches the end of the file and the current input stream does not contain enough bits to write out a complete matrix element of the specified precision, fread pads the last byte or element with zero bits until the full value is obtained. If an error occurs, reading is done up to the last full value. Precision is a string representing the numeric precision of the values read, precision controls the number of bits read for each value and the interpretation of those bits as an integer, a floating-point value, or a character. The precision string may contain a positive integer repetition factor of the form 'n.' which prepends one of the strings above, like '40.uchar'. If precision is not specified, the default 'uchar' (8-bit unsigned character) is assumed.
See for more information. A,count = fread(fid,size, precision,skip) includes an optional skip argument that specifies the number of bytes to skip after each precision value is read. With the skip argument present, fread reads in one value and does a skip of input, reads in another value and does a skip of input, etc. For at most size times. This is useful for extracting data in noncontiguous fields from fixed length records.
If precision is a bit format like 'bitN' or 'ubitN', skip is specified in bits. Remarks Numeric precisions can differ depending on how numbers are represented in your computer's architecture, as well as by the type of compiler used to produce executable code for your computer. The tables below give C-compliant, platform-independent numeric precision string formats that you should use whenever you want your code to be portable. For convenience, MATLAB accepts some C and Fortran data type equivalents for the MATLAB precisions listed.