Read binary file 3 bytes at a time

Hi, I am new and I discovered that the fread by default reads a binary file 1 byte at a time, how can I read the file 3 bytes at a time. Thanks

 Respuesta aceptada

James Tursa
James Tursa el 10 de Jun. de 2015
Editada: James Tursa el 10 de Jun. de 2015
Try something like this. It reads in the entire file as uint8, groups them in 3 bytes, inserts an extra 0 byte in front of the group of 3, and then reinterprets the bytes as 4 byte unsigned integers. If you don't get the result you want, we may have endian issues to deal with and may need to swap bytes.
x = fread(fid,inf,'*uint8'); % read as uint8
x = reshape(x,3,[]); % groups of 3 in columns
[m,n] = size(x); % get size
x = [zeros(1,n,'uint8'); x]; % insert row of 0's at top to get 4 bytes per number
x = typecast(x(:),'uint32'); % reinterpret bytes as 4-byte unsigned integers

4 comentarios

Domenico Bellantoni
Domenico Bellantoni el 10 de Jun. de 2015
matlab gives me this error as soon as it executes the first line
Error using fread Invalid precision.
James Tursa
James Tursa el 10 de Jun. de 2015
This is a typo that I am ALWAYS making and will ALWAYS make throughout my life ... unit8 should be uint8.
Domenico Bellantoni
Domenico Bellantoni el 10 de Jun. de 2015
ahha ok, It was also my fault, I copied and pasted it without checking. Ok now it goes through but it stops at the last line with this error:
Error using typecast The first input argument must be a vector.
James Tursa
James Tursa el 10 de Jun. de 2015
Sorry, you grabbed it before my last edit using x(:) in the last line.

Iniciar sesión para comentar.

Más respuestas (2)

Guillaume
Guillaume el 10 de Jun. de 2015
A much simpler solution would have been to read the file with a precision of 'ubit24' (or 'bit24' if the integer are signed), which is basically a 3 byte integer:
x = fread(fid, 'ubit24'); %that's it. all done

1 comentario

James Tursa
James Tursa el 11 de Jun. de 2015
Good one ... I learned something today about formats available for fread.

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 10 de Jun. de 2015
fread(fid, 3)

4 comentarios

Domenico Bellantoni
Domenico Bellantoni el 10 de Jun. de 2015
This reads only the first 3 values
Domenico Bellantoni
Domenico Bellantoni el 10 de Jun. de 2015
I want to read the full 3 bytes
James Tursa
James Tursa el 10 de Jun. de 2015
Editada: James Tursa el 10 de Jun. de 2015
Do you mean you have a file of many bytes, and each group of 3 bytes in the file should be read in as an individual number? If so, what type of number? An integer of some sort?
Domenico Bellantoni
Domenico Bellantoni el 10 de Jun. de 2015
Editada: Domenico Bellantoni el 10 de Jun. de 2015
@James , yes! Just like that! They are unsigned integer.for each line of the file there is an integer number represented using 3 bytes and I want to read it

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 10 de Jun. de 2015

Comentada:

el 11 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by