How to use fread to read little endian data?
44 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
James McGinley
el 4 de Abr. de 2019
Comentada: Cris LaPierre
el 4 de Abr. de 2019
I have a .jsf file that I am trying to read which starts off with a 16 byte common message header followed by a 240 byte message 80 specific header and then the data that I am interested in. This data changes for each row of the file. EX) row 7 = 4496 bytes, row 8 = 18704 bytes. If I want to read this data row by row, would I need to use a fseek to position myself 256 bytes into the data to start reading the 4496 bytes or the 18704 bytes and then loop it until the end of the file? The data I am reading will then be fed through image().
FileID = fopen('FileName', 'rb')
while ~feof(FileID)
tline = fgetl(FileID)
Start = fseek( tline, 256, 'cof')
Read = fread(Start)
end
0 comentarios
Respuesta aceptada
Cris LaPierre
el 4 de Abr. de 2019
Editada: Cris LaPierre
el 4 de Abr. de 2019
I would seek first (outside the while loop).
Depending on your data, fgetl may not make sense (files read by bytes don't have end-of-lines). I would think just using fread would do it. You do need to know what the precision is. For example, this will read 4 bytes as a float32.
data1 = fread(fid,1,'float32');
4 comentarios
Cris LaPierre
el 4 de Abr. de 2019
I'd just get the data into MATLAB first. Once it's in, you can loop through the data to display it however you want.
Also, be sure once all the data has been read in to close the file:
fclose(fid);
Más respuestas (1)
Jeremy Hughes
el 4 de Abr. de 2019
The control of byte order is on the creation of the file identifier.https://www.mathworks.com/help/matlab/ref/fopen.html#btrnibn-1-machinefmt
fid = fopen('FileName', 'rb','l')
0 comentarios
Ver también
Categorías
Más información sobre Low-Level File I/O en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!