From: Chris Siebern on
I'm a new user to Matlab. I am reading a binary file into an array in Matlab. I need clarification for the fread function regarding the interaction between the 'count' and 'precision' arguments.

My understanding is that when if have open the file and specified little-endian, the count arguement increments byte by byte when the precision argument is not specified. If the precision argument is specified, for example, 'int16' which is a 2 byte data type, does the count increment automatically step in a 2 byte increment to match the data type?

In other words, if I specify the count to be 1 and the precision to be 'int16', will fread read the next 2 bytes, or just 1 byte in the fid?

Thanks for your help.
From: Jan Simon on
Dear Chris!

> In other words, if I specify the count to be 1 and the precision to be 'int16', will fread read the next 2 bytes, or just 1 byte in the fid?

You can simply try it.
FID = fopen(FileName, 'rb');
ftell(FID)
fread(FID, 1, 'int16');
ftell(FID)

You will see, that FREAD reads 1 object of type int16, so the file pointer moves 2 bytes ahead.

Thanks for this clear question! This is my favourite post of the week. I cannot answer 'yes' or 'no', but you have spent time to present two different possible answers.

Kind regards, Jan
From: Walter Roberson on
Chris Siebern wrote:

> In other words, if I specify the count to be 1 and the precision to be
> 'int16', will fread read the next 2 bytes, or just 1 byte in the fid?

The count is the number of items, not the number of bytes.
Likewise the count returned by fread is the number of items actually read.
From: us on
"Chris Siebern" <csiebern(a)purdue.edu> wrote in message <hog056$2ch$1(a)fred.mathworks.com>...
> I'm a new user to Matlab. I am reading a binary file into an array in Matlab. I need clarification for the fread function regarding the interaction between the 'count' and 'precision' arguments.

a hint:
- these are very(!) clearly documented in...

help fread;

us
From: Chris Siebern on
Thanks for the explanations. Understanding that count does not increment in Bytes, but items or objects makes the operation of the function very clear to me. My data now makes sense as I see it in the array :).

I also learned how to implement the ftell function as a bonus!

Thank you Matlab community!