Index: sm/trunk/externalpackages/export_fig/change_value.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/change_value.m	(revision 10634)
+++ 	(revision )
@@ -1,66 +1,0 @@
-%CHANGE_VALUE  Change the value assigned to a unique variable in a file
-%
-% Examples:
-%   fail = change_value(value)
-%   fail = change_value(value, variableName)
-%   fail = change_value(value, variableName, filePath)
-%
-% Function to change the value assigned to a variable in a text file. The
-% assignment must exist already, and must be on a line on its own. For
-% example:
-%    variableName = 'string';
-%    variableName = -0.756e-8;
-% Note that there must be one or more spaces either side of the = sign.
-% Only the first such assignment is changed.
-%
-% IN:
-%   value - The value to be assigned to the variable.
-%   variableName - String containing the name of the variable whose value
-%                  is to be set. Default: name of variable given as value.
-%   filePath - Full path of the file to change. Default: path of calling
-%              file.
-%
-% OUT:
-%   fail - true if change failed, false otherwise.
-
-function fail = change_value(value, variableName, filePath)
-% Check for missing inputs
-if nargin < 3
-    % Get the filename of the calling function
-    filePath = dbstack;
-    filePath = which(filePath(2).file);
-    if nargin < 2
-        % Get the variable name
-        variableName = inputname(1);
-    end
-end
-fail = true;
-% Read in the file
-fh = fopen(filePath, 'rt');
-if fh < 0
-    return
-end
-fstrm = fread(fh, '*char')';
-fclose(fh);
-% Find the path
-first_sec = regexp(fstrm, ['[\n\r]+ *' variableName ' += +'], 'end', 'once');
-second_sec = first_sec + regexp(fstrm(first_sec+1:end), ';? *[\n\r]+', 'once');
-if isempty(first_sec) || isempty(second_sec)
-    return
-end
-% Create the changed line
-if ischar(value)
-    str = '''%s''';
-else
-    str = '%1.50g';
-end
-str = sprintf(str, value);
-% Save the file with the value changed
-fh = fopen(filePath, 'wt');
-if fh < 0
-    return
-end
-fprintf(fh, '%s%s%s', fstrm(1:first_sec), str, fstrm(second_sec:end));
-fclose(fh);
-fail = false;
-return
Index: /issm/trunk/externalpackages/export_fig/export_fig.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/export_fig.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/export_fig.m	(revision 10635)
@@ -7,4 +7,5 @@
 %   export_fig filename -format1 -format2
 %   export_fig ... -nocrop
+%   export_fig ... -transparent
 %   export_fig ... -native
 %   export_fig ... -m<val>
@@ -41,7 +42,9 @@
 % (the latter approximately, and ignoring cropping & magnification) in the
 % output file. For transparent background (and semi-transparent patch
-% objects), set the figure (and axes) 'Color' property to 'none'; pdf, eps
-% and png are the only file formats to support a transparent background,
-% whilst the png format alone supports transparency of patch objects.
+% objects), use the -transparent option or set the figure 'Color' property
+% to 'none'. To make axes transparent set the axes 'Color' property to
+% 'none'. Pdf, eps and png are the only file formats to support a
+% transparent background, whilst the png format alone supports transparency
+% of patch objects.
 %
 % The choice of renderer (opengl, zbuffer or painters) has a large impact
@@ -78,4 +81,6 @@
 %   -nocrop - option indicating that the borders of the output are not to
 %             be cropped.
+%   -transparent - option indicating that the figure background is to be
+%                  made transparent (png, pdf and eps output only).
 %   -m<val> - option where val indicates the factor to magnify the
 %             on-screen figure dimensions by when generating bitmap
@@ -115,5 +120,7 @@
 %             exists, the figure is to be appended as a new page, instead
 %             of being overwritten (default).
-%   handle - The handle of the figure or axes to be saved. Default: gcf.
+%   handle - The handle of the figure or axes (can be an array of handles
+%            of several axes, but these must be in the same figure) to be
+%            saved. Default: gcf.
 %
 %OUT:
@@ -127,5 +134,5 @@
 %   See also PRINT, SAVEAS.
 
-% Copyright (C) Oliver Woodford 2008-2010
+% Copyright (C) Oliver Woodford 2008-2011
 
 % The idea of using ghostscript is inspired by Peder Axensten's SAVEFIG
@@ -153,8 +160,8 @@
 [fig options] = parse_args(nargout, varargin{:});
 % Isolate the subplot, if it is one
-cls = strcmp(get(fig, 'Type'), 'axes');
+cls = strcmp(get(fig(1), 'Type'), 'axes');
 if cls
-    % Given a handle of a single set of axes
-    fig = isolate_subplot(fig);
+    % Given handles of one or more axes, so isolate them from the rest
+    fig = isolate_axes(fig);
 else
     old_mode = get(fig, 'InvertHardcopy');
@@ -193,6 +200,5 @@
 if isbitmap(options)
     % Get the background colour
-    tcol = get(fig, 'Color');
-    if isequal(tcol, 'none') && (options.png || options.alpha)
+    if options.transparent && (options.png || options.alpha)
         % Get out an alpha channel
         % MATLAB "feature": black colorbar axes can change to white and vice versa!
@@ -216,4 +222,5 @@
         % Set the background colour to black, and set size in case it was
         % changed internally
+        tcol = get(fig, 'Color');
         set(fig, 'Color', 'k', 'Position', pos);
         % Correct the colorbar axes colours
@@ -223,5 +230,5 @@
         B = print2array(fig, magnify, renderer);
         % Downscale the image
-        B = downsize(single(B), 0, options.aa_factor);
+        B = downsize(single(B), options.aa_factor);
         % Set background to white (and set size)
         set(fig, 'Color', 'w', 'Position', pos);
@@ -232,9 +239,9 @@
         A = print2array(fig, magnify, renderer);
         % Downscale the image
-        A = downsize(single(A), 255, options.aa_factor);
+        A = downsize(single(A), options.aa_factor);
         % Set the background colour (and size) back to normal
-        set(fig, 'Color', 'none', 'Position', pos);
+        set(fig, 'Color', tcol, 'Position', pos);
         % Compute the alpha map
-        alpha = sum(B - A, 3) / (255*3) + 1;
+        alpha = round(sum(B - A, 3)) / (255 * 3) + 1;
         A = alpha;
         A(A==0) = 1;
@@ -281,18 +288,15 @@
     else
         % Print large version to array
-        if isequal(tcol, 'none')
+        if options.transparent
             % MATLAB "feature": apparently figure size can change when changing
             % colour in -nodisplay mode
             pos = get(fig, 'Position');
+            tcol = get(fig, 'Color');
             set(fig, 'Color', 'w', 'Position', pos);
             A = print2array(fig, magnify, renderer);
-            set(fig, 'Color', 'none', 'Position', pos);
+            set(fig, 'Color', tcol, 'Position', pos);
             tcol = 255;
         else
-            A = print2array(fig, magnify, renderer);
-            tcol = tcol * 255;
-            if ~isequal(tcol, round(tcol))
-                tcol = squeeze(A(1,1,:));
-            end
+            [A tcol] = print2array(fig, magnify, renderer);
         end
         % Crop the background
@@ -301,5 +305,5 @@
         end
         % Downscale the image
-        A = downsize(A, tcol, options.aa_factor);
+        A = downsize(A, options.aa_factor);
         if options.colourspace == 2
             % Convert to greyscale
@@ -378,10 +382,14 @@
         % Generate an eps
         print2eps(tmp_nam, fig, p2eArgs{:});
+        % Remove the background, if desired
+        if options.transparent && ~isequal(get(fig, 'Color'), 'none')
+            eps_remove_background(tmp_nam);
+        end
         % Generate a pdf
         eps2pdf(tmp_nam, pdf_nam, 1, options.append, options.colourspace==2, options.quality);
-    catch
+    catch ex
         % Delete the eps
         delete(tmp_nam);
-        rethrow(lasterror);
+        rethrow(ex);
     end
     % Delete the eps
@@ -391,10 +399,10 @@
             % Generate an eps from the pdf
             pdf2eps(pdf_nam, [options.name '.eps']);
-        catch
+        catch ex
             if ~options.pdf
                 % Delete the pdf
                 delete(pdf_nam);
             end
-            rethrow(lasterror);
+            rethrow(ex);
         end
         if ~options.pdf
@@ -419,4 +427,5 @@
 options = struct('name', 'export_fig_out', ...
                  'crop', true, ...
+                 'transparent', false, ...
                  'renderer', 0, ... % 0: default, 1: OpenGL, 2: ZBuffer, 3: Painters
                  'pdf', false, ...
@@ -426,9 +435,9 @@
                  'jpg', false, ...
                  'bmp', false, ...
-                 'colourspace', 0, ... % ): RGB/gray, 1: CMYK, 2: gray
+                 'colourspace', 0, ... % 0: RGB/gray, 1: CMYK, 2: gray
                  'append', false, ...
                  'im', nout == 1, ...
                  'alpha', nout == 2, ...
-                 'aa_factor', 4, ...
+                 'aa_factor', 3, ...
                  'magnify', 1, ...
                  'quality', []);
@@ -437,5 +446,5 @@
 % Go through the other arguments
 for a = 1:nargin-1
-    if ishandle(varargin{a})
+    if all(ishandle(varargin{a}))
         fig = varargin{a};
     elseif ischar(varargin{a}) && ~isempty(varargin{a})
@@ -444,4 +453,6 @@
                 case 'nocrop'
                     options.crop = false;
+                case {'trans', 'transparent'}
+                    options.transparent = true;
                 case 'opengl'
                     options.renderer = 1;
@@ -489,10 +500,24 @@
             end
         else
-            name = varargin{a};
-            if numel(name) > 3 && name(end-3) == '.' && any(strcmpi(name(end-2:end), {'pdf', 'eps', 'png', 'tif', 'jpg', 'bmp'}))
-                options.(lower(name(end-2:end))) = true;
-                name = name(1:end-4);
+            [p options.name ext] = fileparts(varargin{a});
+            if ~isempty(p)
+                options.name = [p filesep options.name];
             end
-            options.name = name;
+            switch lower(ext)
+                case {'.tif', '.tiff'}
+                    options.tif = true;
+                case {'.jpg', '.jpeg'}
+                    options.jpg = true;
+                case '.png'
+                    options.png = true;
+                case '.bmp'
+                    options.bmp = true;
+                case '.eps'
+                    options.eps = true;
+                case '.pdf'
+                    options.pdf = true;
+                otherwise
+                    options.name = varargin{a};
+            end
         end
     end
@@ -507,4 +532,9 @@
 if ~isvector(options) && ~isbitmap(options)
     options.png = true;
+end
+
+% Check whether transparent background is wanted (old way)
+if isequal(get(fig, 'Color'), 'none')
+    options.transparent = true;
 end
 
@@ -555,59 +585,5 @@
 return
 
-function fh = isolate_subplot(ah, vis)
-% Isolate the axes in a figure on their own
-% Tag the axes so we can find them in the copy
-old_tag = get(ah, 'Tag');
-set(ah, 'Tag', 'ObjectToCopy');
-% Create a new figure exactly the same as the old one
-fh = copyfig(ancestor(ah, 'figure')); %copyobj(ancestor(ah, 'figure'), 0);
-if nargin < 2 || ~vis
-    set(fh, 'Visible', 'off');
-end
-% Reset the axes tag
-set(ah, 'Tag', old_tag);
-% Get all the axes and gui objects
-axs = get(fh, 'Children');
-% Find the objects to save
-ah = findobj(axs, 'Tag', 'ObjectToCopy');
-if numel(ah) ~= 1
-    close(fh);
-    error('Too many axes found');
-end
-I = true(size(axs));
-I(axs==ah) = false;
-% Set the axes tag to what it should be
-set(ah, 'Tag', old_tag);
-% Keep any legends which overlap the subplot
-ax_pos = get(ah, 'OuterPosition');
-ax_pos(3:4) = ax_pos(3:4) + ax_pos(1:2);
-for ah = findobj(axs, 'Tag', 'legend', '-or', 'Tag', 'Colorbar')'
-    leg_pos = get(ah, 'OuterPosition');
-    leg_pos(3:4) = leg_pos(3:4) + leg_pos(1:2);
-    % Overlap test
-    if leg_pos(1) < ax_pos(3) && leg_pos(2) < ax_pos(4) &&...
-       leg_pos(3) > ax_pos(1) && leg_pos(4) > ax_pos(2)
-        I(axs==ah) = false;
-    end
-end
-% Delete all axes except for the input axes and associated items
-delete(axs(I));
-return
-
-function fh = copyfig(fh)
-% Is there a legend?
-if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
-    % Safe to copy using copyobj
-    fh = copyobj(fh, 0);
-else
-    % copyobj will change the figure, so save and then load it instead
-    tmp_nam = [tempname '.fig'];
-    hgsave(fh, tmp_nam);
-    fh = hgload(tmp_nam);
-    delete(tmp_nam);
-end
-return
-
-function A = downsize(A, padval, factor)
+function A = downsize(A, factor)
 % Downsample an image
 if factor == 1
@@ -621,32 +597,16 @@
     % No image processing toolbox - resize manually
     % Lowpass filter - use Gaussian as is separable, so faster
-    switch factor
-        case 4
-            % sigma: 1.7
-            filt = single([0.0148395 0.0498173 0.118323 0.198829 0.236384 0.198829 0.118323 0.0498173 0.0148395]);
-        case 3
-            % sigma: 1.35
-            filt = single([0.025219 0.099418 0.226417 0.297892 0.226417 0.099418 0.025219]);
-        case 2
-            % sigma: 1.0
-            filt = single([0.054489 0.244201 0.40262 0.244201 0.054489]);
-    end
+    % Compute the 1d Gaussian filter
+    filt = (-factor-1:factor+1) / (factor * 0.6);
+    filt = exp(-filt .* filt);
+    % Normalize the filter
+    filt = single(filt / sum(filt));
+    % Filter the image
     padding = floor(numel(filt) / 2);
-    if numel(padval) == 3 && padval(1) == padval(2) && padval(2) == padval(3)
-        padval = padval(1);
-    end
-    if numel(padval) == 1
-        B = repmat(single(padval), [size(A, 1) size(A, 2)] + (2 * padding));
-    end
     for a = 1:size(A, 3)
-        if numel(padval) == 3
-            B = repmat(single(padval(a)), [size(A, 1) size(A, 2)] + (2 * padding));
-        end
-        B(padding+1:end-padding,padding+1:end-padding) = A(:,:,a);
-        A(:,:,a) = conv2(filt, filt', B, 'valid');
-    end
-    clear B
+        A(:,:,a) = conv2(filt, filt', single(A([ones(1, padding) 1:end repmat(end, 1, padding)],[ones(1, padding) 1:end repmat(end, 1, padding)],a)), 'valid');
+    end
     % Subsample
-    A = A(2:factor:end,2:factor:end,:);
+    A = A(1+floor(mod(end-1, factor)/2):factor:end,1+floor(mod(end-1, factor)/2):factor:end,:);
 end
 return
@@ -719,7 +679,35 @@
     end
 end
-% Crop the background
-A = A(t:b,l:r,:);
-v = [t b l r];
+% Crop the background, leaving one boundary pixel to avoid bleeding on
+% resize
+v = [max(t-1, 1) min(b+1, h) max(l-1, 1) min(r+1, w)];
+A = A(v(1):v(2),v(3):v(4),:);
+return
+
+function eps_remove_background(fname)
+% Remove the background of an eps file
+% Open the file
+fh = fopen(fname, 'r+');
+if fh == -1
+    error('Not able to open file %s.', fname);
+end
+% Read the file line by line
+while true
+    % Get the next line
+    l = fgets(fh);
+    if isequal(l, -1)
+        break; % Quit, no rectangle found
+    end
+    % Check if the line contains the background rectangle
+    if isequal(regexp(l, ' *0 +0 +\d+ +\d+ +rf *[\n\r]+', 'start'), 1)
+        % Set the line to whitespace and quit
+        l(1:regexp(l, '[\n\r]', 'start', 'once')-1) = ' ';
+        fseek(fh, -numel(l), 0);
+        fprintf(fh, l);
+        break;
+    end
+end
+% Close the file
+fclose(fh);
 return
 
Index: /issm/trunk/externalpackages/export_fig/fix_lines.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/fix_lines.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/fix_lines.m	(revision 10635)
@@ -41,7 +41,7 @@
 try
     fstrm = fread(fh, '*char')';
-catch
+catch ex
     fclose(fh);
-    rethrow(lasterror);
+    rethrow(ex);
 end
 fclose(fh);
@@ -113,7 +113,7 @@
 
 % Isolate line style definition section
-first_sec = findstr(fstrm, '% line types:');
+first_sec = strfind(fstrm, '% line types:');
 [second_sec remaining] = strtok(fstrm(first_sec+1:end), '/');
-[dummy remaining] = strtok(remaining, '%');
+[remaining remaining] = strtok(remaining, '%');
 
 % Define the new styles, including the new GR format
@@ -147,7 +147,7 @@
     fprintf(fh, '%s\r', new_style{:});
     fwrite(fh, remaining, 'char*1');
-catch
+catch ex
     fclose(fh);
-    rethrow(lasterror);
+    rethrow(ex);
 end
 fclose(fh);
Index: /issm/trunk/externalpackages/export_fig/ghostscript.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/ghostscript.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/ghostscript.m	(revision 10635)
@@ -29,4 +29,10 @@
 % Windows systems.
 
+% 27/4/11 - Find 64-bit Ghostscript on Windows. Thanks to Paul Durack and
+% Shaun Kline for pointing out the issue
+
+% 4/5/11 - Thanks to David Chorlian for pointing out an alternative
+% location for gs on linux.
+
 % Call ghostscript
 [varargout{1:nargout}] = system(sprintf('"%s" %s', gs_path, cmd));
@@ -36,5 +42,5 @@
 % Return a valid path
 % Start with the currently set path
-path = current_gs_path;
+path = user_string('ghostscript');
 % Check the path works
 if check_gs_path(path)
@@ -43,11 +49,13 @@
 % Check whether the binary is on the path
 if ispc
-    bin = 'gswin32c.exe';
+    bin = {'gswin32c.exe', 'gswin64c.exe'};
 else
-    bin = 'gs';
+    bin = {'gs'};
 end
-if check_store_gs_path(bin)
-    path = bin;
-    return
+for a = 1:numel(bin)
+    path = bin{a};
+    if check_store_gs_path(path)
+        return
+    end
 end
 % Search the obvious places
@@ -59,5 +67,5 @@
         dir_list = dir(default_location);
     end
-    executable = '\bin\gswin32c.exe';
+    executable = {'\bin\gswin32c.exe', '\bin\gswin64c.exe'};
     ver_num = 0;
     % If there are multiple versions, use the newest
@@ -65,16 +73,24 @@
         ver_num2 = sscanf(dir_list(a).name, 'gs%g');
         if ~isempty(ver_num2) && ver_num2 > ver_num
-            path2 = [default_location dir_list(a).name executable];
-            if exist(path2, 'file') == 2
-                path = path2;
-                ver_num = ver_num2;
+            for b = 1:numel(executable)
+                path2 = [default_location dir_list(a).name executable{b}];
+                if exist(path2, 'file') == 2
+                    path = path2;
+                    ver_num = ver_num2;
+                end
             end
         end
     end
+    if check_store_gs_path(path)
+        return
+    end
 else
-    path = '/usr/local/bin/gs';
-end
-if check_store_gs_path(path)
-    return
+    bin = {'/usr/bin/gs', '/usr/local/bin/gs'};
+    for a = 1:numel(bin)
+        path = bin{a};
+        if check_store_gs_path(path)
+            return
+        end
+    end
 end
 % Ask the user to enter the path
@@ -93,14 +109,15 @@
     bin_dir = {'', ['bin' filesep], ['lib' filesep]};
     for a = 1:numel(bin_dir)
-        path = [base bin_dir{a} bin];
-        if exist(path, 'file') == 2
-            break;
+        for b = 1:numel(bin)
+            path = [base bin_dir{a} bin{b}];
+            if exist(path, 'file') == 2
+                if check_store_gs_path(path)
+                    return
+                end
+            end
         end
     end
-    if check_store_gs_path(path)
-        return
-    end
 end
-error('Ghostscript not found.');
+error('Ghostscript not found. Have you installed it from www.ghostscript.com?');
 
 function good = check_store_gs_path(path)
@@ -111,6 +128,6 @@
 end
 % Update the current default path to the path found
-if change_value(path, 'current_gs_path_str', [mfilename('fullpath') '.m'])
-    warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.m.');
+if ~user_string('ghostscript', path)
+    warning('Path to ghostscript installation could not be saved. Enter it manually in ghostscript.txt.');
     return
 end
@@ -122,6 +139,2 @@
 good = good == 0;
 return
-
-function current_gs_path_str = current_gs_path
-current_gs_path_str = 'gs';
-return
Index: /issm/trunk/externalpackages/export_fig/isolate_axes.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/isolate_axes.m	(revision 10635)
+++ /issm/trunk/externalpackages/export_fig/isolate_axes.m	(revision 10635)
@@ -0,0 +1,122 @@
+%ISOLATE_AXES Isolate the specified axes in a figure on their own
+%
+% Examples:
+%   fh = isolate_axes(ah)
+%   fh = isolate_axes(ah, vis)
+%
+% This function will create a new figure containing the axes specified, and
+% also their associated legends and colorbars. The axes specified must all
+% be in the same figure, but they will generally only be a subset of the
+% axes in the figure.
+%
+% IN:
+%    ah - An array of axes handles, which must come from the same figure.
+%    vis - A boolean indicating whether the new figure should be visible.
+%          Default: false.
+%
+% OUT:
+%    fh - The handle of the created figure.
+
+% Copyright (C) Oliver Woodford 2011
+
+% Thank you to Rosella Blatt for reporting a bug to do with axes in GUIs
+
+function fh = isolate_axes(ah, vis)
+% Make sure we have an array of handles
+if ~all(ishandle(ah))
+    error('ah must be an array of handles');
+end
+% Check that the handles are all for axes, and are all in the same figure
+fh = ancestor(ah(1), 'figure');
+nAx = numel(ah);
+for a = 1:nAx
+    if ~strcmp(get(ah(a), 'Type'), 'axes')
+        error('All handles must be axes handles.');
+    end
+    if ~isequal(ancestor(ah(a), 'figure'), fh)
+        error('Axes must all come from the same figure.');
+    end
+end
+% Tag the axes so we can find them in the copy
+old_tag = get(ah, 'Tag');
+if nAx == 1
+    old_tag = {old_tag};
+end
+set(ah, 'Tag', 'ObjectToCopy');
+% Create a new figure exactly the same as the old one
+fh = copyfig(fh); %copyobj(fh, 0);
+if nargin < 2 || ~vis
+    set(fh, 'Visible', 'off');
+end
+% Reset the axes tags
+for a = 1:nAx
+    set(ah(a), 'Tag', old_tag{a});
+end
+% Find the objects to save
+ah = findall(fh, 'Tag', 'ObjectToCopy');
+if numel(ah) ~= nAx
+    close(fh);
+    error('Incorrect number of axes found.');
+end
+% Set the axes tags to what they should be
+for a = 1:nAx
+    set(ah(a), 'Tag', old_tag{a});
+end
+% Keep any legends and colorbars which overlap the subplots
+lh = findall(fh, 'Type', 'axes', '-and', {'Tag', 'legend', '-or', 'Tag', 'Colorbar'});
+nLeg = numel(lh);
+if nLeg > 0
+    ax_pos = get(ah, 'OuterPosition');
+    if nAx > 1
+        ax_pos = cell2mat(ax_pos(:));
+    end
+    ax_pos(:,3:4) = ax_pos(:,3:4) + ax_pos(:,1:2);
+    leg_pos = get(lh, 'OuterPosition');
+    if nLeg > 1;
+        leg_pos = cell2mat(leg_pos);
+    end
+    leg_pos(:,3:4) = leg_pos(:,3:4) + leg_pos(:,1:2);
+    for a = 1:nAx
+            % Overlap test
+            ah = [ah; lh(leg_pos(:,1) < ax_pos(a,3) & leg_pos(:,2) < ax_pos(a,4) &...
+                         leg_pos(:,3) > ax_pos(a,1) & leg_pos(:,4) > ax_pos(a,2))];
+    end
+end
+% Get all the objects in the figure
+axs = findall(fh);
+% Delete everything except for the input axes and associated items
+delete(axs(~ismember(axs, [ah; allchildren(ah); allancestors(ah)])));
+return
+
+function ah = allchildren(ah)
+ah = allchild(ah);
+if iscell(ah)
+    ah = cell2mat(ah);
+end
+ah = ah(:);
+return
+
+function ph = allancestors(ah)
+ph = [];
+for a = 1:numel(ah)
+    h = get(ah(a), 'parent');
+    while h ~= 0
+        ph = [ph; h];
+        h = get(h, 'parent');
+    end
+end
+return
+
+function fh = copyfig(fh)
+% Is there a legend?
+if isempty(findobj(fh, 'Type', 'axes', 'Tag', 'legend'))
+    % Safe to copy using copyobj
+    fh = copyobj(fh, 0);
+else
+    % copyobj will change the figure, so save and then load it instead
+    tmp_nam = [tempname '.fig'];
+    hgsave(fh, tmp_nam);
+    fh = hgload(tmp_nam);
+    delete(tmp_nam);
+end
+return
Index: /issm/trunk/externalpackages/export_fig/license.txt
===================================================================
--- /issm/trunk/externalpackages/export_fig/license.txt	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/license.txt	(revision 10635)
@@ -1,3 +1,3 @@
-Copyright (c) 20010, Oliver Woodford
+Copyright (c) 2011, Oliver Woodford
 All rights reserved.
 
Index: /issm/trunk/externalpackages/export_fig/pdftops.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/pdftops.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/pdftops.m	(revision 10635)
@@ -36,5 +36,5 @@
 % Return a valid path
 % Start with the currently set path
-path = current_xpdf_path;
+path = user_string('pdftops');
 % Check the path works
 if check_xpdf_path(path)
@@ -93,6 +93,6 @@
 end
 % Update the current default path to the path found
-if change_value(path, 'current_xpdf_path_str', [mfilename('fullpath') '.m'])
-    warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.m.');
+if ~user_string('pdftops', path)
+    warning('Path to pdftops executable could not be saved. Enter it manually in pdftops.txt.');
     return
 end
@@ -106,6 +106,2 @@
 good = ~isempty(strfind(message, 'PostScript'));
 return
-
-function current_xpdf_path_str = current_xpdf_path
-current_xpdf_path_str = 'C:\Program Files\xpdf-3.02pl4-win32\pdftops.exe';
-return
Index: /issm/trunk/externalpackages/export_fig/print2array.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/print2array.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/print2array.m	(revision 10635)
@@ -6,4 +6,5 @@
 %   A = print2array(figure_handle, resolution)
 %   A = print2array(figure_handle, resolution, renderer)
+%   [A bcol] = print2array(...)
 %
 % This function outputs a bitmap image of the given figure, at the desired
@@ -22,8 +23,19 @@
 % OUT:
 %   A - MxNx3 uint8 image of the figure.
+%   bcol - 1x3 uint8 vector of the background color
 
-% Copyright (C) Oliver Woodford 2008-2010
+% Copyright (C) Oliver Woodford 2008-2011
 
-function A = print2array(fig, res, renderer)
+% 5/9/2011 Set EraseModes to normal when using opengl or zbuffer renderers.
+% Thanks to Pawel Kocieniewski for reporting the issue.
+
+% 21/9/2011 Bug fix: unit8 -> uint8!
+% Thanks to Tobias Lamour for reporting the issue.
+
+% 14/11/2011 Bug fix: stop using hardcopy(), as it interfered with figure
+% size and erasemode settings. Makes it a bit slower, but more reliable.
+% Thanks to Phil Trinh and Meelis Lootus for reporting the issues.
+
+function [A bcol] = print2array(fig, res, renderer)
 % Generate default input arguments, if needed
 if nargin < 2
@@ -38,11 +50,13 @@
 px = get(fig, 'Position');
 set(fig, 'Units', old_mode);
-px = prod(px(3:4)*res)/1e6;
-if px > 30
+npx = prod(px(3:4)*res)/1e6;
+if npx > 30
     % 30M pixels or larger!
-    warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', px);
+    warning('MATLAB:LargeImage', 'print2array generating a %.1fM pixel image. This could be slow and might also cause memory problems.', npx);
 end
+% Retrieve the background colour
+bcol = get(fig, 'Color');
 % Set the resolution parameter
-res = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
+res_str = ['-r' num2str(ceil(get(0, 'ScreenPixelsPerInch')*res))];
 % Generate temporary file name
 tmp_nam = [tempname '.tif'];
@@ -53,5 +67,5 @@
     try
         % Export to tiff using ghostscript
-        ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
+        ghostscript(['-dEPSCrop -q -dNOPAUSE -dBATCH ' res_str ' -sDEVICE=tiff24nc -sOutputFile="' tmp_nam '" "' tmp_eps '"']);
     catch
         % Delete the intermediate file
@@ -65,8 +79,10 @@
     % Delete the temporary bitmap file
     delete(tmp_nam);
-    % Retrieve the background colour
-    bcol = get(fig, 'Color');
     % Set border pixels to the correct colour
-    if ~isequal(bcol, 'none') && ~isequal(bcol, [1 1 1])
+    if isequal(bcol, 'none')
+        bcol = [];
+    elseif isequal(bcol, [1 1 1])
+        bcol = uint8([255 255 255]);
+    else
         for l = 1:size(A, 2)
             if ~all(reshape(A(:,l,:) == 255, [], 1))
@@ -89,5 +105,5 @@
             end
         end
-        bcol = round(bcol * 255);
+        bcol = median([reshape(A(:,[l r],:), [], size(A, 3)); reshape(A(:,[t b],:), [], size(A, 3))], 1);
         for c = 1:size(A, 3)
             A(:,[1:l-1, r+1:end],c) = bcol(c);
@@ -99,20 +115,43 @@
         renderer = '-opengl';
     end
+    err = false;
     % Set paper size
     old_mode = get(fig, 'PaperPositionMode');
     set(fig, 'PaperPositionMode', 'auto');
     try
-        % Try hardcopy first - undocumented MATLAB function!
-        A = hardcopy(fig, ['-D' renderer(2:end)], res);
-    catch
         % Print to tiff file
-        print(fig, renderer, res, '-dtiff', tmp_nam);
+        print(fig, renderer, res_str, '-dtiff', tmp_nam);
         % Read in the printed file
         A = imread(tmp_nam);
         % Delete the temporary file
         delete(tmp_nam);
+    catch ex
+        err = true;
     end
     % Reset paper size
     set(fig, 'PaperPositionMode', old_mode);
+    % Throw any error that occurred
+    if err
+        rethrow(ex);
+    end
+    % Set the background color
+    if isequal(bcol, 'none')
+        bcol = [];
+    else
+        bcol = bcol * 255;
+        if isequal(bcol, round(bcol))
+            bcol = uint8(bcol);
+        else
+            bcol = squeeze(A(1,1,:));
+        end
+    end
+end
+% Check the output size is correct
+if isequal(res, round(res))
+    px = [px([4 3])*res 3];
+    if ~isequal(size(A), px)
+        % Correct the output size
+        A = A(1:min(end,px(1)),1:min(end,px(2)),:);
+    end
 end
 return
Index: /issm/trunk/externalpackages/export_fig/print2eps.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/print2eps.m	(revision 10634)
+++ /issm/trunk/externalpackages/export_fig/print2eps.m	(revision 10635)
@@ -18,5 +18,5 @@
 %   options - Additional parameter strings to be passed to print.
 
-% Copyright (C) Oliver Woodford 2008-2009
+% Copyright (C) Oliver Woodford 2008-2011
 
 % The idea of editing the EPS file to change line styles comes from Jiro
@@ -24,4 +24,8 @@
 % The idea of changing dash length with line width came from comments on
 % fex id: 5743, but the implementation is mine :)
+
+% 14/11/2011 Fix a MATLAB bug rendering black or white text incorrectly.
+% Thanks to Mathieu Morlighem for reporting the issue and obtaining a fix
+% from TMW.
 
 function print2eps(name, fig, varargin)
@@ -39,6 +43,22 @@
 old_mode = get(fig, 'PaperPositionMode');
 set(fig, 'PaperPositionMode', 'auto');
+% MATLAB bug fix - black and white text can come out inverted sometimes
+% Find the white and black text
+white_text_handles = findobj(fig, 'Type', 'text');
+M = get(white_text_handles, 'Color');
+if iscell(M)
+    M = cell2mat(M);
+end
+M = sum(M, 2);
+black_text_handles = white_text_handles(M == 0);
+white_text_handles = white_text_handles(M == 3);
+% Set the font colors slightly off their correct values
+set(black_text_handles, 'Color', [0 0 0] + eps);
+set(white_text_handles, 'Color', [1 1 1] - eps);
 % Print to eps file
 print(fig, options{:}, name);
+% Reset the font colors
+set(black_text_handles, 'Color', [0 0 0]);
+set(white_text_handles, 'Color', [1 1 1]);
 % Reset paper size
 set(fig, 'PaperPositionMode', old_mode);
@@ -47,5 +67,5 @@
     fix_lines(name);
 catch
-    warning('fix_lines failed. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
+    warning('fix_lines() failed. This is usually because the figure contains a large number of patch objects. Consider exporting to a bitmap format in this case.');
 end
 return
Index: /issm/trunk/externalpackages/export_fig/user_string.m
===================================================================
--- /issm/trunk/externalpackages/export_fig/user_string.m	(revision 10635)
+++ /issm/trunk/externalpackages/export_fig/user_string.m	(revision 10635)
@@ -0,0 +1,84 @@
+%USER_STRING  Get/set a user specific string
+%
+% Examples:
+%   string = user_string(string_name)
+%   saved = user_string(string_name, new_string)
+%
+% Function to get and set a string in a system or user specific file. This
+% enables, for example, system specific paths to binaries to be saved.
+%
+% IN:
+%   string_name - String containing the name of the string required. The
+%                 string is extracted from a file called (string_name).txt,
+%                 stored in the same directory as user_string.m.
+%   new_string - The new string to be saved under the name given by
+%                string_name.
+%
+% OUT:
+%   string - The currently saved string. Default: ''.
+%   saved - Boolean indicating whether the save was succesful
+
+% Copyright (C) Oliver Woodford 2011
+
+% This method of saving paths avoids changing .m files which might be in a
+% version control system. Instead it saves the user dependent paths in
+% separate files with a .txt extension, which need not be checked in to
+% the version control system. Thank you to Jonas Dorn for suggesting this
+% approach.
+
+function string = user_string(string_name, string)
+if ~ischar(string_name)
+    error('string_name must be a string.');
+end
+% Create the full filename
+string_name = fullfile(fileparts(mfilename('fullpath')), '.ignore', [string_name '.txt']);
+if nargin > 1
+    % Set string
+    if ~ischar(string)
+        error('new_string must be a string.');
+    end
+    % Make sure the save directory exists
+    dname = fileparts(string_name);
+    if ~exist(dname, 'dir')
+        % Create the directory
+        try
+            if ~mkdir(dname)                
+                string = false;
+                return
+            end
+        catch
+            string = false;
+            return
+        end
+        % Make it hidden
+        try
+            fileattrib(dname, '+h');
+        catch
+        end
+    end
+    % Write the file
+    fid = fopen(string_name, 'w');
+    if fid == -1
+        string = false;
+        return
+    end
+    try
+        fwrite(fid, string, '*char');
+    catch
+        fclose(fid);
+        string = false;
+        return
+    end
+    fclose(fid);
+    string = true;
+else
+    % Get string
+    fid = fopen(string_name, 'r');
+    if fid == -1
+        string = '';
+        return
+    end
+    string = fread(fid, '*char')';
+    fclose(fid);
+end
+return
