Convert Matlab colormap to Surfer colormap

In the comment section of my last post, Steve asked if I had code to generate a Surfer.clr file from my Matlab colormaps.

Some time ago I did write a simple Matlab .m file to write a colormap to a variable with the correct Surfer format, but at the time I was content to have the variable output to a .txt file, which I would then open in a text editor to add a 1-line header and change the file extension to .clr.

I went back, cleaned up the script, and automated all the formatting. This is my revised code (you may need to change the target directory c:\My Documents\MATLAB):

%% Make a Matlab colormap
% one of the colormaps from my function, Perceptually improved colormaps
%% Initialize variable for Surfer colormap
%reduce to 101 samples
%% Make Surfer colormap
% add R,G,B columnns
for i=1:3 
% add counter and alpha (opacity) columns
%% Create output file 
filename='c:\My Documents\MATLAB\stth4surf.clr'; 
%%  Write Surfer colormap header
fprintf(fileID,'ColorMap 2 1\n');
%% Add the colormap:
dlmwrite('c:\My Documents\MATLAB\stth4surf.clr',...
    sawtooth_surfer,'precision',5,'delimiter','\t', '-append');