Borrar filtros
Borrar filtros

How to specify the output type of an fread(fid...) ?

7 visualizaciones (últimos 30 días)
Ian
Ian el 30 de Nov. de 2018
Comentada: Ian el 30 de Nov. de 2018
fread(...) lets you specify the data type of the data to be read, but appears to always return the results in a matrix of doubles.
d = fread(fid, nc, 'int16');
K>> whos d
Name Size Bytes Class Attributes
d 46080x1 368640 double
That's not a problem for small data amounts, but I need to read files with 2-4 GB files with int16 data and work with it. Reading the a 4 GB file of int16's (2 billion elements) results in a 16 GB array of doubles, which I then need to convert back to int16's, so effectively temporarily uses 18 GB of memory.
Q: is there any way to specify the output data type, or tell fread to return the data in the same data type that it read from the file?
My current work-around is to preallocate an array of int16's, and read the the file in smaller chunks, converting each chunk from doubles back to int16's.
Anybody got a better solution?

Respuesta aceptada

per isakson
per isakson el 30 de Nov. de 2018
Try this little experiment (based on the first example in the doc)
%%
fid = fopen('nine.bin','w');
fwrite(fid,[1:9]);
fclose(fid);
%%
fid = fopen('nine.bin','r');
num = fread( fid, [1,9], 'uint8=>uint8' );
fclose(fid);
and
>> whos num
Name Size Bytes Class Attributes
num 1x9 9 uint8
  1 comentario
Ian
Ian el 30 de Nov. de 2018
Excellent. Thanks. I missed that in the documentation, but indeed it's there if one squints hard enough...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data 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!

Translated by