using fread(fid,[n,inf],prec)

Not necessarily a question...
when using fread(fid,[n,Inf],prec)
if n is defined as a small integer (int8, uint16), then resulting row vector [n, Inf] is restricted to the size of n.
if n = uint16 then the row vector [n, Inf] returns [n, 65535]. When reading a large file, the size of n restricts the number of records that can be read. This took me quite a while to unravel. I think the documentation needs to be more specific to show this limitation. For me, I just made n into a 32 bit integer or larger.

Answers (1)

Walter Roberson
Walter Roberson on 3 May 2019
This is not an fread() issue. This is entirely due to the behaviour of the [] operator when you combine unlike data types, which is documented at https://www.mathworks.com/help/matlab/matlab_prog/valid-combinations-of-unlike-classes.html . Generally speaking, the most restrictive data type is the one that results.
You can see it happening even just with [uint8(123), -5] which gives a uint8 vector of [123, 0] where the -5 has been converted to uint8 and saturated to 0. You can see it with [uint8(123), inf] which gives a uint8 vector of [123, 255] where the inf has been converted to uint8 and saturated to 255.
The conversion of data type due to the [] operator happens before fread receives the input. As far as fread() can tell, the input was always uint8([123 255])

2 Comments

Yes, I figured out the probem. I think Mathworks should modify the documentation of fread to clearly show this limitation, or "feature". It would help infrequent users of MatLAB like me.
Thanks
At the bottom of the fread documentation, you can click on the stars in the "How useful was this information" survey, and if you give it a low rank then it will ask you how it could be improved, which will send a note to the documentation team. Or if you have software support, you can open a case directly suggesting a documentation update.
This is not an fread issue, so I doubt they will change the documentation, but I am not them, so they might surprise me.

Sign in to comment.

Categories

Find more on Functions in Help Center and File Exchange

Products

Asked:

on 3 May 2019

Commented:

on 6 May 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!