Memory File (mfile)

Near drop-in replacement for fopen/fread for parsing binary files. Files are completely into memory
Updated 13 Nov 2007

View License

mfile (memory file) is a MATLAB class for reading binary data from memory.

The class is intended as a near drop-in replacement for fopen, fread, fseek, and ftell. Parsing binary files in MALTAB using the standard fopen and fread can be time consuming. A disk read operation is required for each fread call. the mfile class eliminates the need for multiple read operations by reading the binary file into memory all at once. fread commands using mfile scan the memory array holding the file contents rather than accessing the disk. For complicated file formats, the speed improvement can be significant.


Make sure the directory containing the "@mfile" class directory is in the MATLAB path. Replace "fopen" command with "mfile" commands. Operations available on the "mfile" object are fseek,ftell, and fread.

the fread and fseek commands are MATLAB mex code compiled from C. Binaries are included for 32-bit Windows, 64-bit Linux, and 32-bit Linux. Compiling on other platforms should be simple. Type >> mex fread.c
>> mex fseek.c
from the MATLAB command line. Included binaries are compiled with Matlab R2007b


original code:

% The line below is the original
% call one would use
% for opening the file
% fid=fopen('mydata.dat','r');

% Instead, open a "memory file"

% Read some integer values, 4 ints
% at a time, skipping 32 bytes in
% between and storing the data in
% int32 format
% Go 16 bytes from the beginning of
% the file
% Read some float32 values, 8 floats
% at a time, skipping 32 bytes in
% between and storing the data in
% double format

Cite As

Steven Michael (2024). Memory File (mfile) (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Low-Level File I/O in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!


Version Published Release Notes

Include "*<datatype>" calling convention and support for 64-bit signed & unsigned integers.