Code covered by the BSD License  

Highlights from
Memory File (mfile)

4.0

4.0 | 2 ratings Rate this file 3 Downloads (last 30 days) File Size: 45.66 KB File ID: #16634

Memory File (mfile)

by Steven Michael

 

28 Sep 2007 (Updated 13 Nov 2007)

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

| Watch this File

File Information
Description

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');

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
16 Jul 2008 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

23 Jul 2008 Cecil Yen

It will be perfect if it can handle endianness.

Please login to add a comment or rating.
Updates
13 Nov 2007

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

Tag Activity for this File
Tag Applied By Date/Time
data import Steven Michael 22 Oct 2008 09:28:52
data export Steven Michael 22 Oct 2008 09:28:52
file io memory Steven Michael 22 Oct 2008 09:28:52

Contact us at files@mathworks.com