View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 8.71 KB File ID: #37384 Version: 1.0



Jan Simon (view profile)


Expand and truncate existing files - efficient C-Mex

| Watch this File

File Information


While expanding a file by appending zeros is easy in Matlab, truncating is much harder: Create a new file, copy the contents of the original file until the wanted position, delete original file, rename the new file. For large files this is rather inefficient.

This C-Mex function can expand or truncate a file directly. For expanding it is twice as fast as using FWRITE to append zeros, and the speedup for truncating files grows fast with the file length.

[Status, Msg] = FileResize(File, Bytes, Mode)
  File: File name, Unicode and UNC paths accepted.
  Bytes: Number of bytes.
  Mode: 'set': Absolute size, this is the default.
         'move': Relative to the end, truncate if [Bytes] is negative.

  Status: Scalar DOUBLE, 0 on success, otherwise < 0.
  Msg: Explanations of problems.

This function must be compiled before it can be used, or download a pre-compiled file:

Tested: Matlab 6.5, 7.7, 7.8, 7.13, WinXP/32, Win7/64
        Compiler: LCC3.8, BCC5.5, OWC1.8, MSVC2008/2010
Assumed Compatibility: higher Matlab versions, Linux and MacOS.
Run the unit-test function uTest_FileResize to check validity and speed.

Bugreports and suggestions for improvements are welcome.

MATLAB release MATLAB 7.13 (R2011b)
Other requirements A C-compiler
Tags for This File   Please login to tag files.
Please login to add a comment or rating.

Contact us