Peter Boettcher's MATLAB Stuff

If you're looking for the FAQ for comp.soft-sys.matlab, go here:


This page contains some software and documents I've written for and about MATLAB.  Everything is free for all use, though it may not be sold or otherwise distributed for profit.  There is no warranty of any kind associated with this software/information, either express or implied; everything here is to be used at your own risk.

You are free to make modifications to this software for any purpose, but you must retain any statements of credit and/or disclaimer statements found within.  I request that any modifications that might be useful to others be returned to me for inclusion.  Finally, The MathWorks is welcome to include derivatives of this software in future versions of MATLAB; please contact me about this possibility.

About MEX files

Many of the software files included here are MEX files; that is, they are written in C and are meant to be compiled with the MATLAB MEX compiler and used from within MATLAB.  If there is no binary version a certain MEX file for your operating system, you can probably compile your own by typing mex sourcefile.c from within MATLAB.  Feel free to send me (via email) such missing binaries to be included on this web page for others.


This MEX file prints lots of information about MATLAB variables.  I originally wrote it to explore the details of MATLAB's copy-on-write semantics.  Copy-on-write means that data is shared between variables when possible, and copied only when modified.  For instance, B=A creates only a new variable header for B, which points to the exact same data as A.  If either A or B is modified, a full copy of the data is made.  After placing the MEX file in your MATLAB path (compile the C source if necessary), run headerdump(1) for a simple example.  Just about every possible type of variable is supported, though some in more detail than others.  Try copies of variables, especially when cell arrays or structs are involved.  For instance, try copying a cell array with several elements.  Then modify a single element.  Be creative!

Support for R13 and greater added by Gareth Jones. Thanks!
Download from the headerdump directory


NDFUN is a mex file that operates on vaguely the same principal as CELLFUN.  It treats a N-dimensional double precision array as a set of pages of 2D matrices, and operates on those 2D matrices.  Here's the output of help ndfun for full documentation and examples:

 NDFUN Matrix operations on N-D matrices
    NDFUN treats an N-D matrix of double precision values as a set of pages
    of 2D matrices, and performs various matrix operations on those pages.
    The BLAS and LAPACK routines compiled into MATLAB are used for all these
    operations, so results will be very close, and are usually identical, to
    "native MATLAB results".  Available commands are:
    C = NDFUN('mult', A, B)
    C = NDFUN('backslash', A, B)
    C = NDFUN('inv', A)
    C = NDFUN('eig', A)
    [C, D] = NDFUN('eig', A) 
    C = NDFUN('version')
    The two-argument commands perform operations equivalent to:
        for i=1:N
            C(:,:,i) = A(:,:,i) * B(:,:,i);
    The one-argument command
        for i=1:N
            C(:,:,i) = inv(A(:,:,i));
    Any number of dimensions is supported, but dimensions > 2 must match:
        C = ndfun('mult', rand(4,3,2,2,2), rand(3,1,2,2,2))
    C will have size = [4 1 2 2 2]
    NDFUN will reuse 2D arguments when needed, much like scalar
    operations.  A single 2D matrix can be multiplied (or solved with)
    each 2D page of the other argument.  For instance:
        A = rand(4,3);  B = rand(3,10,100);
        C = ndfun('mult', A, B);
    is equivalent to:
        for i=1:100
            C(:,:,i) = A * B(:,:,i);
    The reverse also works.  These types of operations are especially
    efficient for the backslash operator.

Download from the NDFUN directory.


SPLITSTR is a MEX file that parses a long string into a cell array of short strings.  You can split using any character as a delimiter.

From the comments:

/* splitstr.c: MEX file that splits a string based an arbitrary character
 * C = SPLITSTR(S) splits the string S at newlines and returns a cell
 * array where each element is a word.
 * C = SPLITSTR(S, DELIM) uses the character DELIM as the delimiter.
 * The strings '\n' and '\t' can be used to represent newline and tab
 * characters, respectively.
 * Peter Boettcher <>
 * Copyright 2002
 * Last modified: <Tue Jul 30 11:32:13 2002 by pwb>

Here it is: splitstr.c

AVI Codec Selection

I've modified the avi.c MEX file that is shipped with MATLAB. avi.c is used internally in the avifile routines. My modification allows you to specify 'XXXX' as the compression, which causes a codec selection dialog to appear when addframe is first called. Quality and keyframe parametes set in this dialog override any specified in the call to avifile.

Drop the new avi.c into $MATLABROOT\toolbox\matlab\iofun\@avifile\private (making appropriate backups of avi.c and avi.dll) and compile using:

mex avi.c aviutil.c vfw32.lib user32.lib

I suspect mex must be set up to use Visual C for this to work. Here's the file:
Peter Boettcher <>