Link to home
Start Free TrialLog in
Avatar of JulienVan
JulienVanFlag for France

asked on

How can I optimize binary file reading in Matlab?

As I explained in my last question ( https://www.experts-exchange.com/questions/24268148/How-do-I-read-a-binary-file-with-a-specific-structure-in-Matlab.html , thanks yuk99 for the help), I developed a function to read a binary raw data file in Matlab.

Each data sample is stored in the file in 2 bytes, it ranges from 0 to 4095 (12 bits).
In the 2 bytes corresponding to a data sample, the upper 4 bits contain an annotation information, and the 12 other bits contain the data sample.

My code works fine for files with small size, but I need to read a file with a size of about 500 Mb, and following error occurs:

 ??? Error using ==> fread
 Out of memory. Type HELP MEMORY for your options.

Could someone help me optimize my code to avoid this error?
fid=fopen(FILENAME,'r','ieee-le');
 
% Read all 2 bytes samples in 16 bits integers and retransform values in binary
A2=dec2bin(fread(fid,inf,'uint16','ieee-le'),16);
 
% Close file
fclose(fid);
 
% Create an array containing the 12 bits of data
sample=bin2dec(A2(:,5:16));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of masheik