image thumbnail

Print Graphics to a File

by

 

A GUI for printing figures to a file using EPS, JPEG or PNG format.

ptf(varargin)
function ptf(varargin)

% PTF  Print a figure to a file.
%   PTF(H) writes the figure with handle H to the file FIG<H>.EXT where
%   <H> indicates the (integer) handle value and EXT is the extension
%   corresponding to the specified output file format (see below).
%
%   PTF operates on the current figure.
%
%   PTF(...,PARAM1,VALUE1,PARAM2,VALUE2,...) specifies parameter-value
%   pairs that control various characteristics of the output file. Allowed
%   parameter-value pairs are described below.
%
%   PTF(...,PARAM1,VALUE1,PARAM2,VALUE2,...,SWITCH1,SWITCH2,...) allows
%   for additional switches specifying actions which are described below.
%   Any parameter-value pairs provided after the first switch will be
%   ignored.
%
%   File format parameter-value pairs:
%       'Format' Specifies the output file format and should have one of the
%           following values:
%           'eps'     Encapsulated PostScript
%           'eps2'    Encapsulated level 2 Postscript (default)
%           'jpeg'    JPEG files
%           'png'     Portable Network Graphics
%           'preview' Does not generate an output file but instead creates a
%                     new figure window with a preview of the exported
%                     figure. In this case the FILENAME parameter is
%                     ignored.
%       'Preview' Specifies a preview for EPS files and is one of the
%           strings 'none' or 'tiff' (72dpi). Defaults to 'tiff'.
%       'FileName' Specifies the filename of the output file. If no filename
%           extension has been specified, the extension will automatically
%           be set corresponding to the specified output file format.
%
%   Figure size parameter-value pairs:
%       'Width' Specifies the figure width.
%       'Height' Specifies the figure height.
%           Both 'Width' and 'Height' must be positive scalars. Specifying
%           only one dimension sets the other dimension so that the exported
%           aspect ratio is the same as the figure's current aspect ratio.
%           If neither dimension is specified the size defaults to the width
%           and height from the figure's PaperPosition.
%       'PaperUnits' Specifies the units for 'Width' and 'Height' using one
%           of the following values:
%           'inches'      Units in inches
%           'centimeters' Units in centimeters
%           'normalized'  Maps the lower-left corner of the page to (0,0)
%                         and the upper-right corner to (1,1).
%           'points'      Units in points (=1/72 inch)
%           PaperUnits defaults to the current Matlab settings.
%
%   Rendering parameter-value pairs:
%       'Color' Specifies the color mode using one of the following values:
%           'b/w'  Specifies that lines and text are exported in black and
%                  all other objects in grayscale (default).
%           'gray' Specifies that all objects are exported in grayscale.
%           'cmyk' Specifies that all objects are exported in color using
%                  the CMYK color space.
%       'Resolution' A scalar value which specifies the resolution in
%           dots-per-inch. The default value is determined by the Matlab
%           default (see the PRINT command) except that Simulink, Tiff and
%           Jpeg images are printed in screen resolution by default. A
%           Resolution equal to 0 implies screen resolution.
%       'Renderer' Specifies the renderer to use and must be set to one of
%           the values 'painters', 'zbuffer' or 'opengl'. The default value
%           is determined by the current Matlab settings (see the Renderer
%           property of the figure).
%
%   Font parameter-value pairs:
%       'FontMode' One of the strings 'scaled' or 'fixed'.
%       'FontSize' A positive scalar specifying the font size in points.
%           In 'scaled' mode the FontSize value multiplies with the font
%           size of each text object to obtain the exported font size. In
%           'fixed' mode the FontSize value specifies the font size of all
%           text objects in points.
%           If FontMode is 'scaled' but FontSize is not specified then a
%           scaling factor is computed from the ratio of the size of the
%           exported figure to the size of the actual figure. The minimum
%           font size allowed after scaling is 5 points. If FontMode is
%           'fixed' but FontSize is not specified then the exported font
%           sizes of all text objects is 7 points. The default 'FontMode'
%           setting is 'scaled'.
%       'FontName' A string specifying the font of all text objects and axes
%           tick labels. Defaults to the current Matlab settings.
%       'FontStyle' A string specifying the style using one of the following
%           values:
%           'n'   Normal
%           'b'   Bold face
%           'i'   Italics
%           'b/i' Both italics and bold face.
%           FontStyle defaults to the current Matlab settings.
%       'FontEncoding' One of the strings 'latin1' or 'adobe', specifying
%           the character encoding of the font. The default is the Matlab
%           default (specified in the PRINT command).
%
%   Line parameter-value pairs:
%       'LineMode' One of the strings 'scaled' or 'fixed'.
%       'LineWidth' A positive scalar specifying the line width.
%           The semantics of LineMode and LineWidth are exactly the same as
%           FontMode and FontSize, except that they apply to line widths
%           instead of font sizes. The minimum line width allowed after
%           scaling is 0.5 points. If LineMode is 'fixed' but LineWidth is
%           not specified then the exported line width of all line objects
%           is 1 point. The default LineMode is 'scaled'.
%
%   Switches:
%       '-gui' Opens a GUI menu enabling the user to set all parameter-value
%           pairs. Parameter-value pairs provided in the input argument list
%           are displayed in the menu. Any parameter-value pairs after the
%           -gui switch will be ignored. A file won't be created untill the
%           appropriate button in the menu has been pressed.
%       Other switches are internal GUI callbacks so don't bother.
%
%
%   Examples:
%       ptf('Filename','fig1.eps','Height',3)
%           Exports the current figure to the file named fig1.eps with a
%           height of 3 centimeters (assuming the figure's PaperUnits is
%           centimeters) and an aspect ratio the same as the figure's aspect
%           ratio on screen.
%
%       ptf(h,'FontMode','fixed','FontSize',10,'Color','cmyk')
%           Exports the figure with handle h to fig<h>.eps in color with all
%           text in 10 point fonts. The size of the exported figure is the
%           figure's PaperPosition width and height.
%
%       ptf(gcf,'FileName','fig3.eps','-gui')
%           Opens a GUI menu with a pre-set file name 'fig3.eps'. No further
%           actions take place, no files will be generated nor will there be
%           any other activity.

% J.Ploeg    TNO Automotive    November 29, 2000    [mailto:ploeg@wt.tno.nl]
%                              January 29, 2002
% Based on EXPORTFIG.M by Ben Hinkle [mailto:bhinkle@mathworks.com]

% Default settings are defined in the subfunction LocalDefaults.

if nargin==2 & strcmpi(varargin{1},'-gui') & ~isempty(gcbf)         % GUI menu callback
    GUIswitch=varargin{2};
    LocalProcessGUICallback(gcbf,GUIswitch)
else                                                                % Command line call
    action='LocalExportFig(h_fig,par)';                             % action to be taken after the input processing
    if nargin==0 | (nargin>=1 & ~isnumeric(varargin{1}))
        h_fig=gcf;
        k_param=1;                                                  % parameter-value pair counter
    else
        h_fig=varargin{1};
        if ~ishandle(h_fig) | ~strcmpi(get(h_fig,'type'),'figure')
            error('First argument must be a handle to a figure.');
        end
        k_param=2;
    end
    par=LocalDefaults(h_fig);                                        % load default values

    % Process parameter-value pairs and additional switches:
    k_paraminit=k_param;
    while k_param<=nargin
        if ~ischar(varargin{k_param})
            error('Optional parameter names or switches must be strings.')
        end
        switch lower(varargin{k_param})
        case 'format'
            par.format=lower(varargin{k_param+1});
            if ~any(strcmpi(par.format,{'eps' 'eps2' 'jpeg' 'png' 'preview'}))
                error('Format must be ''eps'', ''eps2'', ''jpeg'', ''png'' or ''preview''.')
            end
        case 'preview'
            par.preview=lower(varargin{k_param+1});
            if ~any(strcmpi(par.preview,{'none' 'tiff'}))
                error('Preview must be ''none'' or ''tiff''.')
            end
        case 'filename'
            par.filename=lower(varargin{k_param+1});
            if ~ischar(par.filename)
                error('Filename must be a string.')
            end
        case 'width'
            par.width=LocalToNum(varargin{k_param+1});
            if ~LocalIsPositiveScalar(par.width)
                error('Width must be a numeric scalar > 0.');
            end
        case 'height'
            par.height=LocalToNum(varargin{k_param+1});
            if ~LocalIsPositiveScalar(par.height)
                error('Height must be a numeric scalar > 0.');
            end
        case 'paperunits'
            par.paperunits=lower(varargin{k_param+1});
            if ~any(strcmpi(par.paperunits,{'inches' 'centimeters' 'normalized' 'points'}))
                error('PaperUnits must be ''inches'', ''centimeters'', ''normalized'' or ''points''.');
            end
        case 'color'
            par.color=lower(varargin{k_param+1});
            if ~any(strcmpi(par.color,{'b/w' 'gray' 'cmyk'}))
                error('Color must be ''bw'', ''gray'' or ''cmyk''.')
            end
        case 'resolution'
            par.resolution=LocalToNum(varargin{k_param+1});
            if ~LocalIsPositiveScalar(par.resolution,'0')
                error('Resolution must be a numeric scalar >= 0.');
            end
        case 'renderer'
            par.renderer=lower(varargin{k_param+1});
            if ~any(strcmpi(par.renderer,{'painters' 'zbuffer' 'opengl'}))
                error('Renderer must be ''painters'', ''zbuffer'' or ''opengl''.')
            end
        case 'fontmode'
            par.fontmode=lower(varargin{k_param+1});
            if ~any(strcmpi(par.fontmode,{'scaled' 'fixed'}))
                error('FontMode must be ''scaled'' or ''fixed''.')
            end
        case 'fontsize'
            par.fontsize=LocalToNum(varargin{k_param+1});
            if ~LocalIsPositiveScalar(par.fontsize)
                error('FontSize must be a numeric scalar > 0.')
            end
        case 'fontname'
            par.fontname=lower(varargin{k_param+1});
            if ~ischar(par.fontname)
                error('Font name must be a string.')
            end
        case 'fontstyle'
            par.fontstyle=lower(varargin{k_param+1});
            if ~any(strcmpi(par.fontstyle,{'n' 'b' 'i' 'b/i'}))
                error('FontStyle must be ''n'', ''b'', ''i'' or ''b/i''.')
            end
        case 'fontencoding'
            par.fontencoding=lower(varargin{k_param+1});
            if ~any(strcmpi(par.fontencoding,{'latin1','adobe'}))
                error('FontEncoding must be ''latin1'' or ''adobe''.');
            end
        case 'linemode'
            par.linemode=lower(varargin{k_param+1});
            if ~any(strcmpi(par.linemode,{'scaled','fixed'}))
                error('LineMode must be ''scaled'' or ''fixed''.');
            end
        case 'linewidth'
            par.linewidth=LocalToNum(varargin{k_param+1});
            if ~LocalIsPositiveScalar(par.linewidth)
                error('LineWidth must be a numeric scalar > 0');
            end
        case '-gui'
            % Put an end to this while-loop and open a new GUI menu:
            if k_param>k_paraminit
                par.defaultflag=0;                                  % values are modified, so no defaults anymore
            end
            h_gui=findall(0,'Tag',[mfilename ' gui ' int2str(h_fig)]);
            if ~isempty(h_gui)
                action='LocalSetGUI(h_gui,par)';                    % alternative action: update the current GUI
            else
                action='LocalSetGUI(LocalCreateGUI(h_fig),par)';    % alternative action: create a new GUI
            end
            k_param=inf;                                            % escape this while-loop
        otherwise
            error(['Unrecognized option: ' num2str(varargin{k_param}) '.'])
        end
        k_param=k_param+2;
    end
    % Finally export the figure, update the GUI or create one:
    eval(action);
end


%=======================================================================
% LocalDefaults
%   Define default settings.
%=======================================================================

function par=LocalDefaults(h_fig)

par=struct('format',      'eps2',...
           'preview',     'tiff',...
           'filename',    ['figure' int2str(h_fig)],...
           'width',       [],...
           'height',      [],...
           'paperunits',  get(h_fig,'PaperUnits'),...
           'color',       'cmyk',...
           'resolution',  [],...
           'renderer',    get(h_fig,'Renderer'),...
           'fontmode',    'scaled',...
           'fontsize',    [],...
           'fontname',    '',...
           'fontstyle',   '',...
           'fontencoding','latin1',...
           'linemode',    'scaled',...
           'linewidth',   [],...
           'defaultflag',  1);          % default flag


%=======================================================================
% LocalExportFig
%   Export the figure to a file using all specified properties.
%=======================================================================

function LocalExportFig(h_fig,par)

allaxes   = findall(h_fig,'type','axes');
allimages = findall(h_fig,'type','image');
alllights = findall(h_fig,'type','light');
alllines  = findall(h_fig,'type','line');
allpatch  = findall(h_fig,'type','patch');
allrect   = findall(h_fig,'type','rectangle');
allsurf   = findall(h_fig,'type','surface');
alltext   = findall(h_fig,'type','text');
allfont   = [alltext;allaxes];
allcolor  = [alllines;alltext;allaxes;alllights];
allmarker = [alllines;allpatch;allsurf];
alledge   = [allpatch;allsurf];
allcdata  = [allimages;allpatch;allsurf];

old.objs=cell(0);
old.props=cell(0);
old.values=cell(0);
printargs=cell(0);

hadError=0;
try
    % Process Format parameters:
    showpreview=strcmpi(par.format,'preview');
    if showpreview
        par.format='png';
        par.filename=[tempname '.png'];
    end
    if strncmp(par.format,'eps',3) & ~strcmpi(par.preview,'none')
        printargs={printargs{:} ['-' par.preview]};
    end

    % Process Figure parameters:
    if isempty(par.paperunits)
        par.paperunits=get(h_fig,'PaperUnits');
    end
    oldpaperunits=get(h_fig,'PaperUnits');
    old=LocalPushOldData(old,h_fig,'PaperUnits',oldpaperunits);
    oldpaperpos=get(h_fig,'PaperPosition');
    old=LocalPushOldData(old,h_fig,'PaperPosition',oldpaperpos);
    % Convert normalized PaperUnits to absolute values (centimeters) to prevent from
    % all kinds of scaling problems: 'normalized' with respect to screen size isn't the
    % same as 'normalized' with respect to paper size, so we run into problems
    % determining the proper aspectratio as well as with 'scaled' FontMode and 'scaled'
    % LineWidth.
    if strcmpi(par.paperunits,'normalized')
        par.paperunits='centimeters';
        set(h_fig,'PaperUnits',par.paperunits)
        papersize=get(h_fig,'PaperSize');
        par.width=par.width*papersize(1);        % empty par.width stays empty this way
        par.height=par.height*papersize(2);      % idem
    else
        set(h_fig,'PaperUnits',par.paperunits)
    end
    figureunits=get(h_fig,'Units');
    set(h_fig,'Units',par.paperunits);
    figurepos=get(h_fig,'Position');
    aspectratio=figurepos(3)/figurepos(4);
    set(h_fig,'Units',figureunits);
    if isempty(par.width) & isempty(par.height)
        paperpos=get(h_fig,'PaperPosition');
        par.width=paperpos(3);
        par.height=paperpos(4);
    elseif isempty(par.width) & ~isempty(par.height)
        par.width=par.height*aspectratio;
    elseif ~isempty(par.width) & isempty(par.height)
        par.height=par.width/aspectratio;
    end
    set(h_fig,'PaperPosition',[0 0 par.width par.height]);
    oldpaperposmode=get(h_fig,'PaperPositionMode');
    old=LocalPushOldData(old,h_fig,'PaperPositionMode',oldpaperposmode);
    set(h_fig,'PaperPositionMode','manual');

    % Process Rendering parameters:
    switch par.color
    case {'b/w','gray'}
        if ~strcmpi(par.color,'b/w') & strncmp(par.format,'eps',3)
            par.format=[par.format(1:3) 'c' par.format(4:end)];
        end
        printargs={printargs{:},['-d' par.format]};

        % Compute and set gray colormap:
        oldcmap=get(h_fig,'Colormap');
        newgrays=0.30*oldcmap(:,1)+0.59*oldcmap(:,2)+0.11*oldcmap(:,3);
        newcmap=[newgrays newgrays newgrays];
        old=LocalPushOldData(old,h_fig,'Colormap',oldcmap);
        set(h_fig,'Colormap',newcmap);

        % Compute and set ColorSpec and CData properties:
        old = LocalUpdateColors(allcolor,'color',old);
        old = LocalUpdateColors(allaxes,'xcolor',old);
        old = LocalUpdateColors(allaxes,'ycolor',old);
        old = LocalUpdateColors(allaxes,'zcolor',old);
        old = LocalUpdateColors(allmarker,'MarkerEdgeColor',old);
        old = LocalUpdateColors(allmarker,'MarkerFaceColor',old);
        old = LocalUpdateColors(alledge,'EdgeColor',old);
        old = LocalUpdateColors(alledge,'FaceColor',old);
        old = LocalUpdateColors(allcdata,'CData',old);
    case 'cmyk'
        if strncmp(par.format,'eps',3)
            par.format=[par.format(1:3) 'c' par.format(4:end)];
            printargs={printargs{:} ['-d' par.format] '-cmyk'};
        else
            printargs={printargs{:} ['-d' par.format]};
        end
    otherwise
            error('Invalid Color parameter');
    end

    if ~isempty(par.resolution) | ~strncmp(par.format,'eps',3)
        if isempty(par.resolution) | showpreview
            par.resolution=0;
        end
        printargs={printargs{:} ['-r' int2str(par.resolution)]};
    end

    if ~isempty(par.renderer)
        printargs={printargs{:} ['-' par.renderer]};
    end

    % Process Font parameters:
    oldfonts=LocalGetAsCell(allfont,'FontSize');
    switch par.fontmode
    case 'fixed'
        oldfontunits=LocalGetAsCell(allfont,'FontUnits');
        old=LocalPushOldData(old,allfont,{'FontUnits'},oldfontunits);
        set(allfont,'FontUnits','points');
        if isempty(par.fontsize)
            set(allfont,'FontSize',7);
        else
            set(allfont,'FontSize',par.fontsize);
        end
    case 'scaled'
        if isempty(par.fontsize)
            wscale=par.width/figurepos(3);
            hscale=par.height/figurepos(4);
            scale=min(wscale, hscale);
        else
            scale=par.fontsize;
        end
        newfonts=LocalScale(oldfonts,scale,5);
        set(allfont,{'FontSize'},newfonts);
    otherwise
        error('Invalid FontMode parameter');
    end
    % Make sure we push the size after the units:
    old=LocalPushOldData(old,allfont,{'FontSize'},oldfonts);

    if ~isempty(par.fontname)
        oldfonts=LocalGetAsCell(allfont,'FontName');
        old=LocalPushOldData(old,allfont,{'FontName'},oldfonts);
        set(allfont,'FontName',par.fontname)
    end

    if ~isempty(par.fontstyle)
        oldfonts=LocalGetAsCell(allfont,{'FontAngle' 'FontWeight'});
        old=LocalPushOldData(old,allfont,{'FontAngle' 'FontWeight'},oldfonts);
        if strcmpi(par.fontstyle,'n')
            set(allfont,{'FontWeight' 'FontAngle'},{'normal' 'normal'})
        elseif strcmpi(par.fontstyle,'b')
            set(allfont,{'FontWeight' 'FontAngle'},{'bold' 'normal'})
        elseif strcmpi(par.fontstyle,'i')
            set(allfont,{'FontWeight' 'FontAngle'},{'normal' 'italic'})
        else
            set(allfont,{'FontWeight' 'FontAngle'},{'bold' 'italic'})
        end
    end

    if strcmpi(par.fontencoding,'adobe') & strncmp(par.format,'eps',3)
        printargs={printargs{:} '-adobecset'};
    end

    % Process Line parameters:
    if ~isempty(par.linemode)
        oldlines=LocalGetAsCell(allmarker,'LineWidth');
        old=LocalPushOldData(old,allmarker,{'LineWidth'},oldlines);
        switch par.linemode
        case 'fixed'
            if isempty(par.linewidth)
                set(allmarker,'LineWidth',1);
            else
                set(allmarker,'LineWidth',par.linewidth);
            end
        case 'scaled'
            if isempty(par.linewidth)
                wscale=par.width/figurepos(3);
                hscale=par.height/figurepos(4);
                scale=min(wscale,hscale);
            else
                scale=par.linewidth;
            end
            newlines=LocalScale(oldlines,scale,0.5);
            set(allmarker,{'LineWidth'},newlines);
        otherwise
            error('Invalid LineMode parameter');
        end
    end

    oldresizefcn=get(h_fig,'ResizeFcn');
    old=LocalPushOldData(old,h_fig,'ResizeFcn',oldresizefcn);
    set(h_fig,'ResizeFcn','')                               % PRINT command might issue a warning at a non-empty ResizeFcn

    % Export figure:
    [dum1,dum2,ext]=fileparts(par.filename);
    if isempty(ext)
        if any(strncmp(par.format,'eps',3))
            ext='eps';
        elseif strcmpi(par.format,'jpeg')
            ext='jpg';
        else
            ext='png';
        end
        par.filename=[par.filename '.' ext];
    end
    if ~isempty(gcbf) & exist(fullfile(cd,par.filename))    % GUI callback
        button=questdlg({['File ' par.filename ' already exists.'] 'Overwrite?'},mfilename,'Yes','No','Yes');
        switch lower(button)
        case 'yes'
            print(h_fig,par.filename,printargs{:});
        case 'No'
            % keep menu open
        end
    else                                                    % command line call
        print(h_fig,par.filename,printargs{:});
    end

catch
    hadError=1;
end

% First restore all figure settings ...
for j=1:length(old.objs)
    set(old.objs{j},old.props{j},old.values{j});
end

% ... and then issue a possible error message:
if hadError
    error(deblank(lasterr));
end

% Show preview if requested:
if showpreview
    delete(findall(0,'Type','figure','Tag',[mfilename ' preview ' int2str(h_fig)]))
    if isempty(get(h_fig,'DeleteFcn'))
        set(h_fig,'DeleteFcn',['close(findall(0,''Tag'',''' mfilename ' preview ' int2str(h_fig) '''))'])
    end
    drawnow
    X=imread(par.filename,'png');
    delete(par.filename)
    f=figure('Name',['Preview Figure No. ' int2str(h_fig)], ...
             'Menubar','none', ...
             'NumberTitle','off', ...
             'Resize','off',...
             'Tag',[mfilename ' preview ' int2str(h_fig)], ...
             'Visible','off');
    image(X)
    axis image
    ax=findobj(f,'type','axes');
    set(ax,'Units',par.paperunits, ...
           'Position',[0 0 par.width par.height], ...
           'Visible','off')
    set(ax,'Units','pixels')
    set(f,'Units','pixels')
    matlabunits=get(0,'Units');
    set(0,'Units','pixels')
    axespos=get(ax,'Position');
    figpos=get(f,'Position');
    rootsize=get(0,'ScreenSize');
    figpos(3:4)=axespos(3:4);
    posadapt=figpos(1:2)+figpos(3:4)+[0 50]>rootsize(3:4);
    figpos(1:2)=(rootsize(3:4)-figpos(3:4)-[50 50])*diag(posadapt)+figpos(1:2)*diag(~posadapt);
    set(f,'Position',figpos,...
          'HandleVisibility','callback',...
          'Visible','on')
    set(0,'Units',matlabunits)
end


%=======================================================================
% LocalToNum
%   Converts the input to a numerical value.
%=======================================================================

function value=LocalToNum(value)

if ischar(value)
    value=str2num(value);
end


%=======================================================================
% LocalIsPositiveScalar
%   Test whether the input is a positive scalar or not. 0 and/or empty
%   is allowed, depending on the second input argument.
%=======================================================================

function bool=LocalIsPositiveScalar(value,setting)

if nargin==1
    setting='1';
end
criterium(1)=isnumeric(value);
if isempty(value) & any(strcmp(setting,'[]'))
    criterium(2:3)=[1 1];
else
    criterium(2)=(prod(size(value))==1);
    if any(strcmp(setting,'0'))
        criterium(3)=(value>=0);
    else
        criterium(3)=(value>0);
    end
end
bool=all(criterium);


%=======================================================================
% LocalPushOldData
%   Appends object handles and parameter-value pairs to the
%   corresponding fields of a structure.
%=======================================================================

function outdata=LocalPushOldData(indata,obj,prop,value)

outdata.objs={indata.objs{:} obj};
outdata.props={indata.props{:} prop};
outdata.values={indata.values{:} value};


%=======================================================================
% LocalUpdateColors
%   Maps all colors to shades of gray.
%=======================================================================

function outdata=LocalUpdateColors(inarray,prop,indata)

values=LocalGetAsCell(inarray,prop);
outdata.objs={indata.objs{:} inarray};
outdata.props={indata.props{:} {prop}};
outdata.values={indata.values{:} values};
if ~isempty(values)
    n=length(values);
    newvalues=cell(n,1);
    if strcmpi(prop,'CData')
        % Map Color Data (Image, Patch or Surface) to shades of gray
        for k=1:n
            color=values{k};
            if ndims(color)==3 & isa(color,'double')
                gray=0.30*color(:,:,1)+0.59*color(:,:,2)+0.11*color(:,:,3);
                color(:,:,1)=gray;
                color(:,:,2)=gray;
                color(:,:,3)=gray;
            end
            newvalues{k}=color;
        end
    else
        % Map color RGB values to shades of gray
        for k=1:n
            color=values{k};
            if ~isempty(color)
                if ischar(color)
                    switch color(1)
                    case 'y'
                        color=[1 1 0];
                    case 'm'
                        color=[1 0 1];
                    case 'c'
                        color=[0 1 1];
                    case 'r'
                        color=[1 0 0];
                    case 'g'
                        color=[0 1 0];
                    case 'b'
                        color=[0 0 1];
                    case 'w'
                        color=[1 1 1];
                    case 'k'
                        color=[0 0 0];
                    otherwise
                        newvalues{k}=color;
                    end
                end
                if ~ischar(color)
                    color=0.30*color(1)+0.59*color(2)+0.11*color(3);
                end
            end
            if isempty(color) | ischar(color)
                newvalues{k}=color;
            else
                newvalues{k}=[color color color];
            end
        end
    end
    set(inarray,{prop},newvalues);
end


%=======================================================================
% LocalGetAsCell
%   Puts property values in a cell array (the output of the GET command
%   is not always a cell array).
%=======================================================================

function cellarray=LocalGetAsCell(fig,prop)

cellarray=get(fig,prop);
if ~(isempty(cellarray) | iscell(cellarray))
    cellarray={cellarray};
end


%=======================================================================
% LocalScale
%   Scale font size and linewidth when FontMode and LineMode are set
%   to 'scaled'.
%=======================================================================

function newarray=LocalScale(inarray,scale,minvalue)

n=length(inarray);
newarray=cell(n,1);
for k=1:n
    newarray{k}=max(minvalue,scale*inarray{k});
end


%=======================================================================
% LocalProcessGUICallback
%   Carry out a GUI callback command.
%=======================================================================

function LocalProcessGUICallback(h_gui,GUIswitch)

h=get(h_gui,'UserData');

switch GUIswitch
case '-format'
    % Disable TIFF preview & Adobe character set encoding for file format Jpeg & PNG:
    if any(get(h(1),'Value')==[3 4])
        set(h(2),{'Enable' 'Value'},{'off' 0})
        set(h(14),{'Enable' 'Value'},{'off' 1})
    else
        set(h(2),'Enable','on')
        set(h(14),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-enabledefaults'
    set(h(17),'Enable','on')
case '-checkfilename'
    str=get(h(3),'String');
    if isempty(str)
        warndlg({'You might consider giving this file a name.' 'Otherwise the filename will consist of an extension only!'},mfilename,'modal')
    end
    set(h(17),'Enable','on')
case '-checkwidth'
    str=get(h(4),'String');
    value=str2num(str);
    if ~strcmp(num2str(value),str) | ~LocalIsPositiveScalar(value,'[]') % first test is to make sure that this field is empty or contains a 'stringed' scalar
        set(h(4),'ForegroundColor',[1 0 0],'FontWeight','bold')
        set(h(setdiff([1:22],[4 17 20 21])),'Enable','off')
        errordlg({'Width must be a numeric scalar > 0.' 'Leave the field empty for the default width.'},mfilename,'modal')
    else
        set(h(4),'ForegroundColor',[0 0 0],'FontWeight','normal')
        set(h(setdiff([1:22],[4 17 20 21])),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-checkheight'
    str=get(h(5),'String');
    value=str2num(str);
    if ~strcmp(num2str(value),str) | ~LocalIsPositiveScalar(value,'[]')
        set(h(5),'ForegroundColor',[1 0 0],'FontWeight','bold')
        set(h(setdiff([1:22],[5 17 20 21])),'Enable','off')
        errordlg({'Height must be a numeric scalar > 0.' 'Leave the field empty for the default height.'},mfilename,'modal')
    else
        set(h(5),'ForegroundColor',[0 0 0],'FontWeight','normal')
        set(h(setdiff([1:22],[5 17 20 21])),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-checkresolution'
    str=get(h(8),'String');
    value=str2num(str);
    if ~strcmp(num2str(value),str) | ~LocalIsPositiveScalar(value,{'[]' '0'})
        set(h(8),'ForegroundColor',[1 0 0],'FontWeight','bold')
        set(h(setdiff([1:22],[8 17 20 21])),'Enable','off')
        errordlg({'Resolution must be a numeric scalar >= 0.' 'Leave the field empty for the default resolution.'},mfilename,'modal')
    else
        set(h(8),'ForegroundColor',[0 0 0],'FontWeight','normal')
        set(h(setdiff([1:22],[8 17 20 21])),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-renderer'
    % Disable Resolution for rendering method Painters:
    if get(h(9),'Value')==2
        set(h(8),{'Enable' 'String'},{'off' '<not applicable>'})
    else
        if strcmp(get(h(8),'String'),'<not applicable>')
            set(h(8),{'Enable' 'String'},{'on' ''})
        end
    end
    set(h(17),'Enable','on')
case '-checkfontsize'
    str=get(h(11),'String');
    value=str2num(str);
    if ~strcmp(num2str(value),str) | ~LocalIsPositiveScalar(value,'[]')
        set(h(11),'ForegroundColor',[1 0 0],'FontWeight','bold')
        set(h(setdiff([1:22],[11 17 20 21])),'Enable','off')
        errordlg({'Font size must be a numeric scalar > 0.' 'Leave the field empty for the default font size.'},mfilename,'modal')
    else
        set(h(11),'ForegroundColor',[0 0 0],'FontWeight','normal')
        set(h(setdiff([1:22],[11 17 20 21])),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-checklinewidth'
    str=get(h(16),'String');
    value=str2num(str);
    if ~strcmp(num2str(value),str) | ~LocalIsPositiveScalar(value,'[]')
        set(h(16),'ForegroundColor',[1 0 0],'FontWeight','bold')
        set(h(setdiff([1:22],[16 17 20 21])),'Enable','off')
        errordlg({'Line width must be a numeric scalar > 0.' 'Leave the field empty for the default width.'},mfilename,'modal')
    else
        set(h(16),'ForegroundColor',[0 0 0],'FontWeight','normal')
        set(h(setdiff([1:22],[16 17 20 21])),'Enable','on')
    end
    set(h(17),'Enable','on')
case '-loaddefaults'
    h_fig=h(end);
    par=LocalDefaults(h_fig);
    LocalSetGUI(h_gui,par)
    set(h(setdiff([1:22],[4 17 20 21])),'Enable','on')
case {'-preview' '-ok' '-apply'}
    par=LocalGetGUI(h_gui);
    if strcmp(GUIswitch,'-preview')
        par.format='preview';
    end
    h_fig=h(end);
    enbl=get(h(1:22),'Enable');
    set(h(1:22),'Enable','off')
    set(h([23 24]),'Visible','on')
    LocalExportFig(h_fig,par)
    set(h([23 24]),'Visible','off')
    set(h(1:22),{'Enable'},enbl)
    if strcmp(GUIswitch,'-ok')
        close(h_gui)
    end
case '-cancel'
    close(h_gui)
case '-help'
    helpwin(mfilename)
otherwise
    errordlg(['Unrecognized option: ' GUIswitch '.'],mfilename,'modal')
end


%=======================================================================
% LocalSetGUI
%   Fill the GUI menu with parameter-value settings.
%=======================================================================

function LocalSetGUI(h_gui,par)

h=get(h_gui,'UserData');

set(h(1),'Value',find(strcmpi(par.format,get(h(1),'UserData'))))
set(h(2),'Value',find(strcmpi(par.preview,get(h(2),'UserData')))-1)
set(h(3),'String',par.filename)
set(h(4),'String',int2str(par.width))
set(h(5),'String',int2str(par.height))
set(h(6),'Value',find(strcmpi(par.paperunits,get(h(6),'UserData'))))
set(h(7),'Value',find(strcmpi(par.color,get(h(7),'UserData'))))
set(h(8),'String',int2str(par.resolution))
set(h(9),'Value',find(strcmpi(par.renderer,get(h(9),'UserData'))))
set(h(10),'Value',find(strcmpi(par.fontmode,get(h(10),'UserData'))))
set(h(11),'String',int2str(par.fontsize))
set(h(12),'String',par.fontname)
set(h(13),'Value',find(strcmpi(par.fontstyle,get(h(13),'UserData'))))
set(h(14),'Value',find(strcmpi(par.fontencoding,get(h(14),'UserData'))))
set(h(15),'Value',find(strcmpi(par.linemode,get(h(15),'UserData'))))
set(h(16),'String',int2str(par.linewidth))

% Force some callbacks:
LocalProcessGUICallback(h_gui,'-format')
LocalProcessGUICallback(h_gui,'-renderer')

% Disable/enable the Defaults button:
if par.defaultflag
    set(h(17),'Enable','off')
else
    set(h(17),'Enable','on')
end

% Bring to front:
figure(h_gui)


%=======================================================================
% LocalGetGUI
%   Retrieve all GUI menu parameter-value settings.
%=======================================================================

function par=LocalGetGUI(h_gui)

h=get(h_gui,'UserData');
par=struct('format','','preview','','filename','',...
           'width',[],'height',[],'paperunits','',...
           'color','','resolution',[],'renderer','',...
           'fontmode','','fontsize',[],'fontname','','fontstyle','','fontencoding','',...
           'linemode','','linewidth',[]);

% Get all parameter values of the popupmenus at once:
allvalues=get(h(1:end-1),'Userdata');   % last handle is the handle of the figure to be exported

% All validity checking on parameter-value pairs has already been done by the GUI callbacks!
par.format       = allvalues{1}{get(h(1),'Value')};
par.preview      = allvalues{2}{get(h(2),'Value')+1};
par.filename     = get(h(3),'String');
par.width        = str2num(get(h(4),'String'));
par.height       = str2num(get(h(5),'String'));
par.paperunits   = allvalues{6}{get(h(6),'Value')};
par.color        = allvalues{7}{get(h(7),'Value')};
par.resolution   = str2num(get(h(8),'String'));
par.renderer     = allvalues{9}{get(h(9),'Value')};
par.fontmode     = allvalues{10}{get(h(10),'Value')};
par.fontsize     = str2num(get(h(11),'String'));
par.fontname     = get(h(12),'String');
par.fontstyle    = allvalues{13}{get(h(13),'Value')};
par.fontencoding = allvalues{14}{get(h(14),'Value')};
par.linemode     = allvalues{15}{get(h(15),'Value')};
par.linewidth    = str2num(get(h(16),'String'));


%=======================================================================
% LocalCreateGUI
%   Create the GUI menu.
%=======================================================================

function varargout=LocalCreateGUI(h_fig)

% Set properties and position of menu figure:
OrgUnits=get(0,{'Units' 'DefaultFigureUnits'});
set(0,{'Units' 'DefaultFigureUnits'},{'pixels' 'pixels'})
screen=get(0,'ScreenSize');
bgcolor= get(0,'DefaultUicontrolBackgroundColor');
editcolor=[255 255 255]/255;
framecolor=max(min(0.65*bgcolor,[1 1 1]),[0 0 0]);
h_gui=figure('Position',[round((screen(3)-515)/2) round((screen(4)-360)/2) 515 360],...
             'NumberTitle','off',...
             'MenuBar','none',...
             'Name',['Print Figure No. ' int2str(h_fig)],...
             'Color',bgcolor,...
             'Tag',[mfilename ' gui ' int2str(h_fig)],...
             'DeleteFcn',['close(findall(0,''Tag'',''' mfilename ' preview ' int2str(h_fig) '''))'],...
             'HandleVisibility','callback',...
             'Visible','on');
set(h_gui,'Resize','off')   % in Windows XP the Resize command must be given separately (?)
% Set default uicontrol properties:
set(h_gui,'DefaultUicontrolUnits','pixels',...
          'DefaultUicontrolBackgroundColor',bgcolor,...
          'DefaultUicontrolForegroundColor',[0 0 0],...
          'DefaultUicontrolFontUnits','pixels',...
          'DefaultUicontrolFontSize',9,...
          'DefaultUicontrolFontAngle','normal',...
          'DefaultUicontrolFontWeight','normal',...
          'DefaultUicontrolFontName','MS Sans Serif',...
          'DefaultUicontrolHorizontalAlignment','left')

h=zeros(24,1);

% File section:
Local3DFrame(h_gui,[10 260 240 90],framecolor,'File')
h(1)=uicontrol('Parent',h_gui,...
               'Style','popupmenu',...
               'Position',[90 320 150 20],...
               'String',{'Encapsulated PostScript' 'Encapsulated Level 2 PS' 'JPEG' 'Portable Network Graphics'},...
               'UserData',{'eps' 'eps2' 'jpeg' 'png'},...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -format']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 317 62 20],...
               'HorizontalAlignment','right',...
               'String','Format',...
               'TooltipString','Graphics file format')
h(2)=uicontrol('Parent',h_gui,...
               'Style','checkbox',...
               'Position',[90 295 150 20],...
               'String','Tiff preview',...
               'UserData',{'none' 'tiff'},...
               'Callback',[mfilename ' -gui -enabledefaults'],...
               'TooltipString','Tiff preview for EPS files in Word');
h(3)=uicontrol('Parent',h_gui,...
               'Style','edit',...
               'Position',[90 270 150 20],...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -checkfilename']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 267 62 20],...
               'HorizontalAlignment','right',...
               'String','Name',...
               'TooltipString','File name (with or without extension)')

% Figure section:
Local3DFrame(h_gui,[10 155 240 90],framecolor,'Figure')
h(4)=uicontrol('Parent',h_gui,...
               'Style','edit',...
               'Position',[90 215 150 20],...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -checkwidth']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 212 62 20],...
               'HorizontalAlignment','right',...
               'String','Width',...
               'TooltipString','Figure width in paper units')
h(5)=uicontrol('Parent',h_gui,...
               'Style','edit',...
               'Position',[90 190 150 20],...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -checkheight']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 187 62 20],...
               'HorizontalAlignment','right',...
               'String','Height',...
               'TooltipString','Figure height in paper units')
h(6)=uicontrol('Parent',h_gui,...
               'Style','popupmenu',...
               'Position',[90 165 150 20],...
               'String',{'inches' 'centimeters' 'normalized' 'points'},...
               'UserData',{'inches' 'centimeters' 'normalized' 'points'},...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -enabledefaults']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 162 62 20],...
               'HorizontalAlignment','right',...
               'String','Paper units',...
               'TooltipString','Paper units of measurement')

% Rendering section:
Local3DFrame(h_gui,[10 50 240 90],framecolor,'Rendering')
h(7)=uicontrol('Parent',h_gui,...
               'Style','popupmenu',...
               'Position',[90 110 150 20],...
               'String',{'black/white' 'shades of gray' 'true color'},...
               'UserData',{'b/w' 'gray' 'cmyk'},...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -enabledefaults']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 107 62 20],...
               'HorizontalAlignment','right',...
               'String','Color',...
               'TooltipString','Figure color style')
h(8)=uicontrol('Parent',h_gui,...
               'Style','edit',...
               'Position',[90 85 150 20],...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -checkresolution']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 82 62 20],...
               'HorizontalAlignment','right',...
               'String','Resolution',...
               'TooltipString','Resolution in dpi (dots per inch)')
h(9)=uicontrol('Parent',h_gui,...
               'Style','popupmenu',...
               'Position',[90 60 150 20],...
               'String',{'Painters' 'Z-Buffer' 'OpenGL'},...
               'UserData',{'painters' 'zbuffer' 'opengl'},...
               'Backgroundcolor',editcolor,...
               'Callback',[mfilename ' -gui -renderer']);
     uicontrol('Parent',h_gui,...
               'Style','text',...
               'Position',[20 57 62 20],...
               'HorizontalAlignment','right',...
               'String','Renderer',...
               'TooltipString','Rendering method')

% Fonts section:
Local3DFrame(h_gui,[265 210 240 140],framecolor,'Fonts')
h(10)=uicontrol('Parent',h_gui,...
                'Style','popupmenu',...
                'Position',[345 320 150 20],...
                'String',{'scaled' 'fixed [points]'},...
                'UserData',{'scaled' 'fixed'},...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -enabledefaults']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 317 62 20],...
                'HorizontalAlignment','right',...
                'String','Mode',...
                'TooltipString','Font size method')
h(11)=uicontrol('Parent',h_gui,...
                'Style','edit',...
                'Position',[345 295 150 20],...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -checkfontsize']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 292 62 20],...
                'HorizontalAlignment','right',...
                'String','Size',...
                'TooltipString','Font size')
h(12)=uicontrol('Parent',h_gui,...
                'Style','edit',...
                'Position',[345 270 150 20],...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -enabledefaults']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 267 62 20],...
                'HorizontalAlignment','right',...
                'String','Name',...
                'TooltipString','Font name')
h(13)=uicontrol('Parent',h_gui,...
                'Style','popupmenu',...
                'Position',[345 245 150 20],...
                'String',{' ' 'normal' 'bold' 'italic' 'bold/italic'},...
                'UserData',{'' 'n' 'b' 'i' 'b/i'},...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -enabledefaults']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 242 62 20],...
                'HorizontalAlignment','right',...
                'String','Style',...
                'TooltipString','Font style')
h(14)=uicontrol('Parent',h_gui,...
                'Style','popupmenu',...
                'Position',[345 220 150 20],...
                'String',{'Latin-1' 'Adobe'},...
                'UserData',{'latin1' 'adobe'},...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -enabledefaults']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 217 62 20],...
                'HorizontalAlignment','right',...
                'String','Encoding',...
                'TooltipString','Character encoding')

% Lines section:
Local3DFrame(h_gui,[265 130 240 65],framecolor,'Lines')
h(15)=uicontrol('Parent',h_gui,...
                'Style','popupmenu',...
                'Position',[345 165 150 20],...
                'String',{'scaled' 'fixed [points]'},...
                'UserData',{'scaled' 'fixed'},...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -enabledefaults']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 162 62 20],...
                'HorizontalAlignment','right',...
                'String','Mode',...
                'TooltipString','Line width method')
h(16)=uicontrol('Parent',h_gui,...
                'Style','edit',...
                'Position',[345 140 150 20],...
                'Backgroundcolor',editcolor,...
                'Callback',[mfilename ' -gui -checklinewidth']);
      uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[275 137 62 20],...
                'HorizontalAlignment','right',...
                'String','Width',...
                'TooltipString','Line width')

% "Defaults" pushbutton:
h(17)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[265 90 115 25],...
                'String','Defaults',...
                'TooltipString','Re-load default settings',...
                'Callback',[mfilename ' -gui -loaddefaults']);
% "Preview" pushbutton:
h(18)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[390 90 115 25],...
                'String','Preview',...
                'TooltipString','Show preview picture',...
                'Callback',[mfilename ' -gui -preview']);
% OK  CANCEL  HELP  UNDO  APPLY  CLOSE
% "OK" pushbutton:
h(19)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[130 10 87 25],...
                'String','OK',...
                'TooltipString','Export figure and close gui',...
                'Callback',[mfilename ' -gui -ok']);
% "Cancel" pushbutton:
h(20)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[226 10 87 25],...
                'String','Cancel',...
                'Callback',[mfilename ' -gui -cancel']);
% "Help" pushbutton:
h(21)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[322 10 87 25],...
                'String','Help',...
                'Callback',[mfilename ' -gui -help']);
% "Apply" pushbutton:
h(22)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[418 10 87 25],...
                'String','Apply',...
                'TooltipString','Export figure but leave gui open',...
                'Callback',[mfilename ' -gui -apply']);
% "Please Wait" window:
h(23)=uicontrol('Parent',h_gui,...
                'Style','pushbutton',...
                'Position',[129 100 257 180],...
                'Enable','off',...
                'Visible','off');
h(24)=uicontrol('Parent',h_gui,...
                'Style','text',...
                'Position',[134 105 247 170],...
                'HorizontalAlignment','center',...
                'FontSize',13,...
                'FontWeight','normal',...
                'String',{'' '' '' 'Generating graphical' 'output.' '' 'Please wait ...'},...
                'Visible','off');

% Make an end to all this overhead:
if isempty(get(h_fig,'DeleteFcn'))                  % set the figure's DeleteFcn to close the GUI when the figure itself is closed
    set(h_fig,'DeleteFcn',['close(findall(0,''Tag'',''' mfilename ' gui ' int2str(h_fig) ''')),',...
                           'close(findall(0,''Tag'',''' mfilename ' preview ' int2str(h_fig) '''));'])
end
set(0,{'Units' 'DefaultFigureUnits'},OrgUnits)      % restore original units settings
set(h_gui,'UserData',[h;h_fig],'Visible','on')      % finally set userdata and display the menu
if nargout
    varargout={h_gui};                              % output GUI handle
end


%=======================================================================
% Local3DFrame
%   Define a frame with a 3D effect and a label.
%=======================================================================

function Local3DFrame(parent,position,color,label)

uicontrol('Parent',parent,...
          'Style','frame',...
          'Position',position+[0 -1 1 1],...
          'ForegroundColor',[1 1 1]);
uicontrol('Parent',parent,...
          'Style','frame',...
          'Position',position,...
          'ForegroundColor',color)
uicontrol('Parent',parent,...
          'Style','frame',...
          'Position',position+[1 1 1-position(3) -2],...
          'ForegroundColor',[1 1 1])
uicontrol('Parent',parent,...
          'Style','frame',...
          'Position',position+[1 position(4)-2 -2 1-position(4)],...
          'ForegroundColor',[1 1 1])
if ~isempty(label)
    ht=uicontrol('Parent',parent,...
                 'Style','Text',...
                 'Position',[10 10 10 10],...
                 'String',[' ' label]);
    textsize=get(ht,'Extent');
    set(ht,'Position',[position(1)+5 sum(position([2 4]))-13 textsize(3) 20])
end

Contact us