File Exchange

image thumbnail

Memory File (mfile)

version 1.0.0.0 (45.7 KB) by Steven Michael
Near drop-in replacement for fopen/fread for parsing binary files. Files are completely into memory

5 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 (2021). Memory File (mfile) (https://www.mathworks.com/matlabcentral/fileexchange/16634-memory-file-mfile), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Cecil Yen

It will be perfect if it can handle endianness.

Jon B

I like the idea -- could be very useful.

A few things are needed to become a drop in replacement:
1) pass varargin directly to the fread call in mfile, which will allow handling of endianness at a file level (or at least detect and send the machineformat and perhaps encoding)
2) Add an fclose(fid) statement at end of mfile.m!
3) Add bitstream support -- ubitN and bitN.

Support for the fopen's machine format (and/or fread) machine format Missing support for

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!

mfile/@mfile/