How can I optimize binary file reading in Matlab?

As I explained in my last question ( , 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?
% Read all 2 bytes samples in 16 bits integers and retransform values in binary
% Close file
% Create an array containing the 12 bits of data

Open in new window

Who is Participating?
yuk99Connect With a Mentor Commented:
This is common problem in Matlab. Read this doc, it may give you some ideas.
yuk99Connect With a Mentor Commented:
Can you actually upgrade? Working with uints will save you some memory.

Another useful doc:
All Courses

From novice to tech pro — start learning today.