function ddfconvert2(datamat, filename)

% basically, you want to call this function like:
%   ddfconvert2(stuff, 'stuff.ddf')
% where stuff is an array with 4 dimensions:
%   stuff(column, row, color, frame)

% for the 1E floor, there are 32 rows, and 16 columns.
% the software is (I think) contrained to 999 frames,
% although an animation that big is going to take up
% a lot of space anyway. 

% be careful to set your start and end frames such that
% the end frame transitions directly to the start frame.

fid=fopen(filename,'w');
if (fid==-1)
 display('you idiot!');
else
    dataout=zeros(1,16*32*3*size(datamat,4));
    j=1;
    for frame=1:size(datamat,4)
       display(strcat(int2str(frame),'/',int2str(size(datamat,4))))
       for row=1:32
           for i=16:-1:1 
               for color=1:3
                   dataout(j)=datamat(i,row,color,frame);
                   j=j+1;
               end
           end
       end      
    end
    if(fwrite(fid,dataout,'uchar')==-1)
        display('aieeeeee!');
    end
    fclose(fid);
end