How to read in binary file data as something other than double datatype?
4 views (last 30 days)
Show older comments
Scott Kilianski
on 1 Sep 2020
Commented: Scott Kilianski
on 1 Sep 2020
I am trying to read in data from a binary file using the fread function (see example below). No matter what precision I specify, the output is always of the datatype 'double'. I want the output to be int16 datatype. I do not want to simply convert the output from a double to int16 - the reason I want to read in the data as int16 is to minimize memory usage. I'm using MATLAB 2020a
fileID = fopen('nine.bin','w');
fwrite(fileID,[1:9],'int16');
fclose(fileID);
fileID = fopen('nine.bin');
A = fread(fileID,[3,2],'int16') %A comes out as 'double' datatype here. I need int16
0 Comments
Accepted Answer
Steven Lord
on 1 Sep 2020
From the description of the precision input on the documentation page for the fread function, specifying the precision as *source means "The input values and the output matrix, A, are of the class specified by source."
A = fread(fileID, [3 2], '*int16');
2 Comments
More Answers (0)
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!