Memory File (mfile)

Near drop-in replacement for fopen/fread for parsing binary files. Files are completely into memory
1.1K Downloads
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.

Usage:

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

Example:

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"
fid=mfile('mydata.dat');

% Read some integer values, 4 ints
% at a time, skipping 32 bytes in
% between and storing the data in
% int32 format
val1=fread(fid,10,...
'4*int32=>int32',32);
% Go 16 bytes from the beginning of
% the file
fseek(fid,16,'bof');
% Read some float32 values, 8 floats
% at a time, skipping 32 bytes in
% between and storing the data in
% double format
val2=fread(fid,8,'single=>double');

Cite As

Steven Michael (2024). Memory File (mfile) (https://www.mathworks.com/matlabcentral/fileexchange/16634-memory-file-mfile), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
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!

mfile/@mfile/

Version Published Release Notes
1.0.0.0

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