annotate _misc/figures/.svn/text-base/exportfig.m.svn-base @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 function varargout = exportfig(varargin)
matthiasm@8 2 %EXPORTFIG Export a figure.
matthiasm@8 3 % EXPORTFIG(H, FILENAME) writes the figure H to FILENAME. H is
matthiasm@8 4 % a figure handle and FILENAME is a string that specifies the
matthiasm@8 5 % name of the output file.
matthiasm@8 6 %
matthiasm@8 7 % EXPORTFIG(H, FILENAME, OPTIONS) writes the figure H to FILENAME
matthiasm@8 8 % with options initially specified by the structure OPTIONS. The
matthiasm@8 9 % field names of OPTIONS must be legal parameters listed below
matthiasm@8 10 % and the field values must be legal values for the corresponding
matthiasm@8 11 % parameter. Default options can be set in releases prior to R12
matthiasm@8 12 % by storing the OPTIONS structure in the root object's appdata
matthiasm@8 13 % with the command
matthiasm@8 14 % setappdata(0,'exportfigdefaults', OPTIONS)
matthiasm@8 15 % and for releases after R12 by setting the preference with the
matthiasm@8 16 % command
matthiasm@8 17 % setpref('exportfig', 'defaults', OPTIONS)
matthiasm@8 18 %
matthiasm@8 19 % EXPORTFIG(...,PARAM1,VAL1,PARAM2,VAL2,...) specifies
matthiasm@8 20 % parameters that control various characteristics of the output
matthiasm@8 21 % file. Any parameter value can be the string 'auto' which means
matthiasm@8 22 % the parameter uses the default factory behavior, overriding
matthiasm@8 23 % any other default for the parameter.
matthiasm@8 24 %
matthiasm@8 25 % Format Paramter:
matthiasm@8 26 % 'Format' a string
matthiasm@8 27 % specifies the output format. Defaults to 'eps'. For a
matthiasm@8 28 % list of export formats type 'help print'.
matthiasm@8 29 % 'Preview' one of the strings 'none', 'tiff'
matthiasm@8 30 % specifies a preview for EPS files. Defaults to 'none'.
matthiasm@8 31 %
matthiasm@8 32 % Size Parameters:
matthiasm@8 33 % 'Width' a positive scalar
matthiasm@8 34 % specifies the width in the figure's PaperUnits
matthiasm@8 35 % 'Height' a positive scalar
matthiasm@8 36 % specifies the height in the figure's PaperUnits
matthiasm@8 37 % 'Bounds' one of the strings 'tight', 'loose'
matthiasm@8 38 % specifies a tight or loose bounding box. Defaults to 'tight'.
matthiasm@8 39 % 'Reference' an axes handle or a string
matthiasm@8 40 % specifies that the width and height parameters
matthiasm@8 41 % are relative to the given axes. If a string is
matthiasm@8 42 % specified then it must evaluate to an axes handle.
matthiasm@8 43 %
matthiasm@8 44 % Specifying only one dimension sets the other dimension
matthiasm@8 45 % so that the exported aspect ratio is the same as the
matthiasm@8 46 % figure's or reference axes' current aspect ratio.
matthiasm@8 47 % If neither dimension is specified the size defaults to
matthiasm@8 48 % the width and height from the figure's or reference
matthiasm@8 49 % axes' size. Tight bounding boxes are only computed for
matthiasm@8 50 % 2-D views and in that case the computed bounds enclose all
matthiasm@8 51 % text objects.
matthiasm@8 52 %
matthiasm@8 53 % Rendering Parameters:
matthiasm@8 54 % 'Color' one of the strings 'bw', 'gray', 'cmyk'
matthiasm@8 55 % 'bw' specifies that lines and text are exported in
matthiasm@8 56 % black and all other objects in grayscale
matthiasm@8 57 % 'gray' specifies that all objects are exported in grayscale
matthiasm@8 58 % 'rgb' specifies that all objects are exported in color
matthiasm@8 59 % using the RGB color space
matthiasm@8 60 % 'cmyk' specifies that all objects are exported in color
matthiasm@8 61 % using the CMYK color space
matthiasm@8 62 % 'Renderer' one of 'painters', 'zbuffer', 'opengl'
matthiasm@8 63 % specifies the renderer to use
matthiasm@8 64 % 'Resolution' a positive scalar
matthiasm@8 65 % specifies the resolution in dots-per-inch.
matthiasm@8 66 % 'LockAxes' one of 0 or 1
matthiasm@8 67 % specifies that all axes limits and ticks should be fixed
matthiasm@8 68 % while exporting.
matthiasm@8 69 %
matthiasm@8 70 % The default color setting is 'bw'.
matthiasm@8 71 %
matthiasm@8 72 % Font Parameters:
matthiasm@8 73 % 'FontMode' one of the strings 'scaled', 'fixed'
matthiasm@8 74 % 'FontSize' a positive scalar
matthiasm@8 75 % in 'scaled' mode multiplies with the font size of each
matthiasm@8 76 % text object to obtain the exported font size
matthiasm@8 77 % in 'fixed' mode specifies the font size of all text
matthiasm@8 78 % objects in points
matthiasm@8 79 % 'DefaultFixedFontSize' a positive scalar
matthiasm@8 80 % in 'fixed' mode specified the default font size in
matthiasm@8 81 % points
matthiasm@8 82 % 'FontSizeMin' a positive scalar
matthiasm@8 83 % specifies the minimum font size allowed after scaling
matthiasm@8 84 % 'FontSizeMax' a positive scalar
matthiasm@8 85 % specifies the maximum font size allowed after scaling
matthiasm@8 86 % 'FontEncoding' one of the strings 'latin1', 'adobe'
matthiasm@8 87 % specifies the character encoding of the font
matthiasm@8 88 % 'SeparateText' one of 0 or 1
matthiasm@8 89 % specifies that the text objects are stored in separate
matthiasm@8 90 % file as EPS with the base filename having '_t' appended.
matthiasm@8 91 %
matthiasm@8 92 % If FontMode is 'scaled' but FontSize is not specified then a
matthiasm@8 93 % scaling factor is computed from the ratio of the size of the
matthiasm@8 94 % exported figure to the size of the actual figure.
matthiasm@8 95 %
matthiasm@8 96 % The default 'FontMode' setting is 'scaled'.
matthiasm@8 97 %
matthiasm@8 98 % Line Width Parameters:
matthiasm@8 99 % 'LineMode' one of the strings 'scaled', 'fixed'
matthiasm@8 100 % 'LineWidth' a positive scalar
matthiasm@8 101 % 'DefaultFixedLineWidth' a positive scalar
matthiasm@8 102 % 'LineWidthMin' a positive scalar
matthiasm@8 103 % specifies the minimum line width allowed after scaling
matthiasm@8 104 % 'LineWidthMax' a positive scalar
matthiasm@8 105 % specifies the maximum line width allowed after scaling
matthiasm@8 106 % The semantics of 'Line' parameters are exactly the
matthiasm@8 107 % same as the corresponding 'Font' parameters, except that
matthiasm@8 108 % they apply to line widths instead of font sizes.
matthiasm@8 109 %
matthiasm@8 110 % Style Map Parameter:
matthiasm@8 111 % 'LineStyleMap' one of [], 'bw', or a function name or handle
matthiasm@8 112 % specifies how to map line colors to styles. An empty
matthiasm@8 113 % style map means styles are not changed. The style map
matthiasm@8 114 % 'bw' is a built-in mapping that maps lines with the same
matthiasm@8 115 % color to the same style and otherwise cycles through the
matthiasm@8 116 % available styles. A user-specified map is a function
matthiasm@8 117 % that takes as input a cell array of line objects and
matthiasm@8 118 % outputs a cell array of line style strings. The default
matthiasm@8 119 % map is [].
matthiasm@8 120 %
matthiasm@8 121 % Examples:
matthiasm@8 122 % exportfig(gcf,'fig1.eps','height',3);
matthiasm@8 123 % Exports the current figure to the file named 'fig1.eps' with
matthiasm@8 124 % a height of 3 inches (assuming the figure's PaperUnits is
matthiasm@8 125 % inches) and an aspect ratio the same as the figure's aspect
matthiasm@8 126 % ratio on screen.
matthiasm@8 127 %
matthiasm@8 128 % opts = struct('FontMode','fixed','FontSize',10,'height',3);
matthiasm@8 129 % exportfig(gcf, 'fig2.eps', opts, 'height', 5);
matthiasm@8 130 % Exports the current figure to 'fig2.eps' with all
matthiasm@8 131 % text in 10 point fonts and with height 5 inches.
matthiasm@8 132 %
matthiasm@8 133 % See also PREVIEWFIG, APPLYTOFIG, RESTOREFIG, PRINT.
matthiasm@8 134
matthiasm@8 135 % Copyright 2000 Ben Hinkle
matthiasm@8 136 % Email bug reports and comments to bhinkle@mathworks.com
matthiasm@8 137
matthiasm@8 138 if (nargin < 2)
matthiasm@8 139 error('Too few input arguments');
matthiasm@8 140 end
matthiasm@8 141
matthiasm@8 142 % exportfig(H, filename, [options,] ...)
matthiasm@8 143 H = varargin{1};
matthiasm@8 144 if ~LocalIsHG(H,'figure')
matthiasm@8 145 error('First argument must be a handle to a figure.');
matthiasm@8 146 end
matthiasm@8 147 filename = varargin{2};
matthiasm@8 148 if ~ischar(filename)
matthiasm@8 149 error('Second argument must be a string.');
matthiasm@8 150 end
matthiasm@8 151 paramPairs = {varargin{3:end}};
matthiasm@8 152 if nargin > 2
matthiasm@8 153 if isstruct(paramPairs{1})
matthiasm@8 154 pcell = LocalToCell(paramPairs{1});
matthiasm@8 155 paramPairs = {pcell{:}, paramPairs{2:end}};
matthiasm@8 156 end
matthiasm@8 157 end
matthiasm@8 158 verstr = version;
matthiasm@8 159 majorver = str2num(verstr(1));
matthiasm@8 160 defaults = [];
matthiasm@8 161 if majorver > 5
matthiasm@8 162 if ispref('exportfig','defaults')
matthiasm@8 163 defaults = getpref('exportfig','defaults');
matthiasm@8 164 end
matthiasm@8 165 elseif exist('getappdata')
matthiasm@8 166 defaults = getappdata(0,'exportfigdefaults');
matthiasm@8 167 end
matthiasm@8 168 if ~isempty(defaults)
matthiasm@8 169 dcell = LocalToCell(defaults);
matthiasm@8 170 paramPairs = {dcell{:}, paramPairs{:}};
matthiasm@8 171 end
matthiasm@8 172
matthiasm@8 173 % Do some validity checking on param-value pairs
matthiasm@8 174 if (rem(length(paramPairs),2) ~= 0)
matthiasm@8 175 error(['Invalid input syntax. Optional parameters and values' ...
matthiasm@8 176 ' must be in pairs.']);
matthiasm@8 177 end
matthiasm@8 178
matthiasm@8 179 auto.format = 'eps';
matthiasm@8 180 auto.preview = 'none';
matthiasm@8 181 auto.width = -1;
matthiasm@8 182 auto.height = -1;
matthiasm@8 183 auto.color = 'bw';
matthiasm@8 184 auto.defaultfontsize=10;
matthiasm@8 185 auto.fontsize = -1;
matthiasm@8 186 auto.fontmode='scaled';
matthiasm@8 187 auto.fontmin = 8;
matthiasm@8 188 auto.fontmax = 60;
matthiasm@8 189 auto.defaultlinewidth = 1.0;
matthiasm@8 190 auto.linewidth = -1;
matthiasm@8 191 auto.linemode=[];
matthiasm@8 192 auto.linemin = 0.5;
matthiasm@8 193 auto.linemax = 100;
matthiasm@8 194 auto.fontencoding = 'latin1';
matthiasm@8 195 auto.renderer = [];
matthiasm@8 196 auto.resolution = [];
matthiasm@8 197 auto.stylemap = [];
matthiasm@8 198 auto.applystyle = 0;
matthiasm@8 199 auto.refobj = -1;
matthiasm@8 200 auto.bounds = 'tight';
matthiasm@8 201 explicitbounds = 0;
matthiasm@8 202 auto.lockaxes = 1;
matthiasm@8 203 auto.separatetext = 0;
matthiasm@8 204 opts = auto;
matthiasm@8 205
matthiasm@8 206 % Process param-value pairs
matthiasm@8 207 args = {};
matthiasm@8 208 for k = 1:2:length(paramPairs)
matthiasm@8 209 param = lower(paramPairs{k});
matthiasm@8 210 if ~ischar(param)
matthiasm@8 211 error('Optional parameter names must be strings');
matthiasm@8 212 end
matthiasm@8 213 value = paramPairs{k+1};
matthiasm@8 214
matthiasm@8 215 switch (param)
matthiasm@8 216 case 'format'
matthiasm@8 217 opts.format = LocalCheckAuto(lower(value),auto.format);
matthiasm@8 218 if strcmp(opts.format,'preview')
matthiasm@8 219 error(['Format ''preview'' no longer supported. Use PREVIEWFIG' ...
matthiasm@8 220 ' instead.']);
matthiasm@8 221 end
matthiasm@8 222 case 'preview'
matthiasm@8 223 opts.preview = LocalCheckAuto(lower(value),auto.preview);
matthiasm@8 224 if ~strcmp(opts.preview,{'none','tiff'})
matthiasm@8 225 error('Preview must be ''none'' or ''tiff''.');
matthiasm@8 226 end
matthiasm@8 227 case 'width'
matthiasm@8 228 opts.width = LocalToNum(value, auto.width);
matthiasm@8 229 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 230 if ~LocalIsPositiveScalar(opts.width)
matthiasm@8 231 error('Width must be a numeric scalar > 0');
matthiasm@8 232 end
matthiasm@8 233 end
matthiasm@8 234 case 'height'
matthiasm@8 235 opts.height = LocalToNum(value, auto.height);
matthiasm@8 236 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 237 if(~LocalIsPositiveScalar(opts.height))
matthiasm@8 238 error('Height must be a numeric scalar > 0');
matthiasm@8 239 end
matthiasm@8 240 end
matthiasm@8 241 case 'color'
matthiasm@8 242 opts.color = LocalCheckAuto(lower(value),auto.color);
matthiasm@8 243 if ~strcmp(opts.color,{'bw','gray','rgb','cmyk'})
matthiasm@8 244 error('Color must be ''bw'', ''gray'',''rgb'' or ''cmyk''.');
matthiasm@8 245 end
matthiasm@8 246 case 'fontmode'
matthiasm@8 247 opts.fontmode = LocalCheckAuto(lower(value),auto.fontmode);
matthiasm@8 248 if ~strcmp(opts.fontmode,{'scaled','fixed'})
matthiasm@8 249 error('FontMode must be ''scaled'' or ''fixed''.');
matthiasm@8 250 end
matthiasm@8 251 case 'fontsize'
matthiasm@8 252 opts.fontsize = LocalToNum(value,auto.fontsize);
matthiasm@8 253 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 254 if ~LocalIsPositiveScalar(opts.fontsize)
matthiasm@8 255 error('FontSize must be a numeric scalar > 0');
matthiasm@8 256 end
matthiasm@8 257 end
matthiasm@8 258 case 'defaultfixedfontsize'
matthiasm@8 259 opts.defaultfontsize = LocalToNum(value,auto.defaultfontsize);
matthiasm@8 260 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 261 if ~LocalIsPositiveScalar(opts.defaultfontsize)
matthiasm@8 262 error('DefaultFixedFontSize must be a numeric scalar > 0');
matthiasm@8 263 end
matthiasm@8 264 end
matthiasm@8 265 case 'fontsizemin'
matthiasm@8 266 opts.fontmin = LocalToNum(value,auto.fontmin);
matthiasm@8 267 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 268 if ~LocalIsPositiveScalar(opts.fontmin)
matthiasm@8 269 error('FontSizeMin must be a numeric scalar > 0');
matthiasm@8 270 end
matthiasm@8 271 end
matthiasm@8 272 case 'fontsizemax'
matthiasm@8 273 opts.fontmax = LocalToNum(value,auto.fontmax);
matthiasm@8 274 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 275 if ~LocalIsPositiveScalar(opts.fontmax)
matthiasm@8 276 error('FontSizeMax must be a numeric scalar > 0');
matthiasm@8 277 end
matthiasm@8 278 end
matthiasm@8 279 case 'fontencoding'
matthiasm@8 280 opts.fontencoding = LocalCheckAuto(lower(value),auto.fontencoding);
matthiasm@8 281 if ~strcmp(opts.fontencoding,{'latin1','adobe'})
matthiasm@8 282 error('FontEncoding must be ''latin1'' or ''adobe''.');
matthiasm@8 283 end
matthiasm@8 284 case 'linemode'
matthiasm@8 285 opts.linemode = LocalCheckAuto(lower(value),auto.linemode);
matthiasm@8 286 if ~strcmp(opts.linemode,{'scaled','fixed'})
matthiasm@8 287 error('LineMode must be ''scaled'' or ''fixed''.');
matthiasm@8 288 end
matthiasm@8 289 case 'linewidth'
matthiasm@8 290 opts.linewidth = LocalToNum(value,auto.linewidth);
matthiasm@8 291 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 292 if ~LocalIsPositiveScalar(opts.linewidth)
matthiasm@8 293 error('LineWidth must be a numeric scalar > 0');
matthiasm@8 294 end
matthiasm@8 295 end
matthiasm@8 296 case 'defaultfixedlinewidth'
matthiasm@8 297 opts.defaultlinewidth = LocalToNum(value,auto.defaultlinewidth);
matthiasm@8 298 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 299 if ~LocalIsPositiveScalar(opts.defaultlinewidth)
matthiasm@8 300 error(['DefaultFixedLineWidth must be a numeric scalar >' ...
matthiasm@8 301 ' 0']);
matthiasm@8 302 end
matthiasm@8 303 end
matthiasm@8 304 case 'linewidthmin'
matthiasm@8 305 opts.linemin = LocalToNum(value,auto.linemin);
matthiasm@8 306 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 307 if ~LocalIsPositiveScalar(opts.linemin)
matthiasm@8 308 error('LineWidthMin must be a numeric scalar > 0');
matthiasm@8 309 end
matthiasm@8 310 end
matthiasm@8 311 case 'linewidthmax'
matthiasm@8 312 opts.linemax = LocalToNum(value,auto.linemax);
matthiasm@8 313 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 314 if ~LocalIsPositiveScalar(opts.linemax)
matthiasm@8 315 error('LineWidthMax must be a numeric scalar > 0');
matthiasm@8 316 end
matthiasm@8 317 end
matthiasm@8 318 case 'linestylemap'
matthiasm@8 319 opts.stylemap = LocalCheckAuto(value,auto.stylemap);
matthiasm@8 320 case 'renderer'
matthiasm@8 321 opts.renderer = LocalCheckAuto(lower(value),auto.renderer);
matthiasm@8 322 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 323 if ~strcmp(opts.renderer,{'painters','zbuffer','opengl'})
matthiasm@8 324 error(['Renderer must be ''painters'', ''zbuffer'' or' ...
matthiasm@8 325 ' ''opengl''.']);
matthiasm@8 326 end
matthiasm@8 327 end
matthiasm@8 328 case 'resolution'
matthiasm@8 329 opts.resolution = LocalToNum(value,auto.resolution);
matthiasm@8 330 if ~ischar(value) | ~strcmp(value,'auto')
matthiasm@8 331 if ~(isnumeric(value) & (prod(size(value)) == 1) & (value >= 0));
matthiasm@8 332 error('Resolution must be a numeric scalar >= 0');
matthiasm@8 333 end
matthiasm@8 334 end
matthiasm@8 335 case 'applystyle' % means to apply the options and not export
matthiasm@8 336 opts.applystyle = 1;
matthiasm@8 337 case 'reference'
matthiasm@8 338 if ischar(value)
matthiasm@8 339 if strcmp(value,'auto')
matthiasm@8 340 opts.refobj = auto.refobj;
matthiasm@8 341 else
matthiasm@8 342 opts.refobj = eval(value);
matthiasm@8 343 end
matthiasm@8 344 else
matthiasm@8 345 opts.refobj = value;
matthiasm@8 346 end
matthiasm@8 347 if ~LocalIsHG(opts.refobj,'axes')
matthiasm@8 348 error('Reference object must evaluate to an axes handle.');
matthiasm@8 349 end
matthiasm@8 350 case 'bounds'
matthiasm@8 351 opts.bounds = LocalCheckAuto(lower(value),auto.bounds);
matthiasm@8 352 explicitbounds = 1;
matthiasm@8 353 if ~strcmp(opts.bounds,{'tight','loose'})
matthiasm@8 354 error('Bounds must be ''tight'' or ''loose''.');
matthiasm@8 355 end
matthiasm@8 356 case 'lockaxes'
matthiasm@8 357 opts.lockaxes = LocalToNum(value,auto.lockaxes);
matthiasm@8 358 case 'separatetext'
matthiasm@8 359 opts.separatetext = LocalToNum(value,auto.separatetext);
matthiasm@8 360 otherwise
matthiasm@8 361 error(['Unrecognized option ' param '.']);
matthiasm@8 362 end
matthiasm@8 363 end
matthiasm@8 364
matthiasm@8 365 % make sure figure is up-to-date
matthiasm@8 366 drawnow;
matthiasm@8 367
matthiasm@8 368 allLines = findall(H, 'type', 'line');
matthiasm@8 369 allText = findall(H, 'type', 'text');
matthiasm@8 370 allAxes = findall(H, 'type', 'axes');
matthiasm@8 371 allImages = findall(H, 'type', 'image');
matthiasm@8 372 allLights = findall(H, 'type', 'light');
matthiasm@8 373 allPatch = findall(H, 'type', 'patch');
matthiasm@8 374 allSurf = findall(H, 'type', 'surface');
matthiasm@8 375 allRect = findall(H, 'type', 'rectangle');
matthiasm@8 376 allFont = [allText; allAxes];
matthiasm@8 377 allColor = [allLines; allText; allAxes; allLights];
matthiasm@8 378 allMarker = [allLines; allPatch; allSurf];
matthiasm@8 379 allEdge = [allPatch; allSurf];
matthiasm@8 380 allCData = [allImages; allPatch; allSurf];
matthiasm@8 381
matthiasm@8 382 old.objs = {};
matthiasm@8 383 old.prop = {};
matthiasm@8 384 old.values = {};
matthiasm@8 385
matthiasm@8 386 % Process format
matthiasm@8 387 if strncmp(opts.format,'eps',3) & ~strcmp(opts.preview,'none')
matthiasm@8 388 args = {args{:}, ['-' opts.preview]};
matthiasm@8 389 end
matthiasm@8 390
matthiasm@8 391 hadError = 0;
matthiasm@8 392 oldwarn = warning;
matthiasm@8 393 try
matthiasm@8 394
matthiasm@8 395 % lock axes limits, ticks and labels if requested
matthiasm@8 396 if opts.lockaxes
matthiasm@8 397 old = LocalManualAxesMode(old, allAxes, 'TickMode');
matthiasm@8 398 old = LocalManualAxesMode(old, allAxes, 'TickLabelMode');
matthiasm@8 399 old = LocalManualAxesMode(old, allAxes, 'LimMode');
matthiasm@8 400 end
matthiasm@8 401
matthiasm@8 402 % Process size parameters
matthiasm@8 403 figurePaperUnits = get(H, 'PaperUnits');
matthiasm@8 404 oldFigureUnits = get(H, 'Units');
matthiasm@8 405 oldFigPos = get(H,'Position');
matthiasm@8 406 set(H, 'Units', figurePaperUnits);
matthiasm@8 407 figPos = get(H,'Position');
matthiasm@8 408 refsize = figPos(3:4);
matthiasm@8 409 if opts.refobj ~= -1
matthiasm@8 410 oldUnits = get(opts.refobj, 'Units');
matthiasm@8 411 set(opts.refobj, 'Units', figurePaperUnits);
matthiasm@8 412 r = get(opts.refobj, 'Position');
matthiasm@8 413 refsize = r(3:4);
matthiasm@8 414 set(opts.refobj, 'Units', oldUnits);
matthiasm@8 415 end
matthiasm@8 416 aspectRatio = refsize(1)/refsize(2);
matthiasm@8 417 if (opts.width == -1) & (opts.height == -1)
matthiasm@8 418 opts.width = refsize(1);
matthiasm@8 419 opts.height = refsize(2);
matthiasm@8 420 elseif (opts.width == -1)
matthiasm@8 421 opts.width = opts.height * aspectRatio;
matthiasm@8 422 elseif (opts.height == -1)
matthiasm@8 423 opts.height = opts.width / aspectRatio;
matthiasm@8 424 end
matthiasm@8 425 wscale = opts.width/refsize(1);
matthiasm@8 426 hscale = opts.height/refsize(2);
matthiasm@8 427 sizescale = min(wscale,hscale);
matthiasm@8 428 old = LocalPushOldData(old,H,'PaperPositionMode', ...
matthiasm@8 429 get(H,'PaperPositionMode'));
matthiasm@8 430 set(H, 'PaperPositionMode', 'auto');
matthiasm@8 431 newPos = [figPos(1) figPos(2)+figPos(4)*(1-hscale) ...
matthiasm@8 432 wscale*figPos(3) hscale*figPos(4)];
matthiasm@8 433 set(H, 'Position', newPos);
matthiasm@8 434 set(H, 'Units', oldFigureUnits);
matthiasm@8 435
matthiasm@8 436 % process line-style map
matthiasm@8 437 if ~isempty(opts.stylemap) & ~isempty(allLines)
matthiasm@8 438 oldlstyle = LocalGetAsCell(allLines,'LineStyle');
matthiasm@8 439 old = LocalPushOldData(old, allLines, {'LineStyle'}, ...
matthiasm@8 440 oldlstyle);
matthiasm@8 441 newlstyle = oldlstyle;
matthiasm@8 442 if ischar(opts.stylemap) & strcmpi(opts.stylemap,'bw')
matthiasm@8 443 newlstyle = LocalMapColorToStyle(allLines);
matthiasm@8 444 else
matthiasm@8 445 try
matthiasm@8 446 newlstyle = feval(opts.stylemap,allLines);
matthiasm@8 447 catch
matthiasm@8 448 warning(['Skipping stylemap. ' lasterr]);
matthiasm@8 449 end
matthiasm@8 450 end
matthiasm@8 451 set(allLines,{'LineStyle'},newlstyle);
matthiasm@8 452 end
matthiasm@8 453
matthiasm@8 454 % Process rendering parameters
matthiasm@8 455 switch (opts.color)
matthiasm@8 456 case {'bw', 'gray'}
matthiasm@8 457 if ~strcmp(opts.color,'bw') & strncmp(opts.format,'eps',3)
matthiasm@8 458 opts.format = [opts.format 'c'];
matthiasm@8 459 end
matthiasm@8 460 args = {args{:}, ['-d' opts.format]};
matthiasm@8 461
matthiasm@8 462 %compute and set gray colormap
matthiasm@8 463 oldcmap = get(H,'Colormap');
matthiasm@8 464 newgrays = 0.30*oldcmap(:,1) + 0.59*oldcmap(:,2) + 0.11*oldcmap(:,3);
matthiasm@8 465 newcmap = [newgrays newgrays newgrays];
matthiasm@8 466 old = LocalPushOldData(old, H, 'Colormap', oldcmap);
matthiasm@8 467 set(H, 'Colormap', newcmap);
matthiasm@8 468
matthiasm@8 469 %compute and set ColorSpec and CData properties
matthiasm@8 470 old = LocalUpdateColors(allColor, 'color', old);
matthiasm@8 471 old = LocalUpdateColors(allAxes, 'xcolor', old);
matthiasm@8 472 old = LocalUpdateColors(allAxes, 'ycolor', old);
matthiasm@8 473 old = LocalUpdateColors(allAxes, 'zcolor', old);
matthiasm@8 474 old = LocalUpdateColors(allMarker, 'MarkerEdgeColor', old);
matthiasm@8 475 old = LocalUpdateColors(allMarker, 'MarkerFaceColor', old);
matthiasm@8 476 old = LocalUpdateColors(allEdge, 'EdgeColor', old);
matthiasm@8 477 old = LocalUpdateColors(allEdge, 'FaceColor', old);
matthiasm@8 478 old = LocalUpdateColors(allCData, 'CData', old);
matthiasm@8 479
matthiasm@8 480 case {'rgb','cmyk'}
matthiasm@8 481 if strncmp(opts.format,'eps',3)
matthiasm@8 482 opts.format = [opts.format 'c'];
matthiasm@8 483 args = {args{:}, ['-d' opts.format]};
matthiasm@8 484 if strcmp(opts.color,'cmyk')
matthiasm@8 485 args = {args{:}, '-cmyk'};
matthiasm@8 486 end
matthiasm@8 487 else
matthiasm@8 488 args = {args{:}, ['-d' opts.format]};
matthiasm@8 489 end
matthiasm@8 490 otherwise
matthiasm@8 491 error('Invalid Color parameter');
matthiasm@8 492 end
matthiasm@8 493 if (~isempty(opts.renderer))
matthiasm@8 494 args = {args{:}, ['-' opts.renderer]};
matthiasm@8 495 end
matthiasm@8 496 if (~isempty(opts.resolution)) | ~strncmp(opts.format,'eps',3)
matthiasm@8 497 if isempty(opts.resolution)
matthiasm@8 498 opts.resolution = 0;
matthiasm@8 499 end
matthiasm@8 500 args = {args{:}, ['-r' int2str(opts.resolution)]};
matthiasm@8 501 end
matthiasm@8 502
matthiasm@8 503 % Process font parameters
matthiasm@8 504 if ~isempty(opts.fontmode)
matthiasm@8 505 oldfonts = LocalGetAsCell(allFont,'FontSize');
matthiasm@8 506 oldfontunits = LocalGetAsCell(allFont,'FontUnits');
matthiasm@8 507 set(allFont,'FontUnits','points');
matthiasm@8 508 switch (opts.fontmode)
matthiasm@8 509 case 'fixed'
matthiasm@8 510 if (opts.fontsize == -1)
matthiasm@8 511 set(allFont,'FontSize',opts.defaultfontsize);
matthiasm@8 512 else
matthiasm@8 513 set(allFont,'FontSize',opts.fontsize);
matthiasm@8 514 end
matthiasm@8 515 case 'scaled'
matthiasm@8 516 if (opts.fontsize == -1)
matthiasm@8 517 scale = sizescale;
matthiasm@8 518 else
matthiasm@8 519 scale = opts.fontsize;
matthiasm@8 520 end
matthiasm@8 521 newfonts = LocalScale(oldfonts,scale,opts.fontmin,opts.fontmax);
matthiasm@8 522 set(allFont,{'FontSize'},newfonts);
matthiasm@8 523 otherwise
matthiasm@8 524 error('Invalid FontMode parameter');
matthiasm@8 525 end
matthiasm@8 526 old = LocalPushOldData(old, allFont, {'FontSize'}, oldfonts);
matthiasm@8 527 old = LocalPushOldData(old, allFont, {'FontUnits'}, oldfontunits);
matthiasm@8 528 end
matthiasm@8 529 if strcmp(opts.fontencoding,'adobe') & strncmp(opts.format,'eps',3)
matthiasm@8 530 args = {args{:}, '-adobecset'};
matthiasm@8 531 end
matthiasm@8 532
matthiasm@8 533 % Process line parameters
matthiasm@8 534 if ~isempty(opts.linemode)
matthiasm@8 535 oldlines = LocalGetAsCell(allMarker,'LineWidth');
matthiasm@8 536 old = LocalPushOldData(old, allMarker, {'LineWidth'}, oldlines);
matthiasm@8 537 switch (opts.linemode)
matthiasm@8 538 case 'fixed'
matthiasm@8 539 if (opts.linewidth == -1)
matthiasm@8 540 set(allMarker,'LineWidth',opts.defaultlinewidth);
matthiasm@8 541 else
matthiasm@8 542 set(allMarker,'LineWidth',opts.linewidth);
matthiasm@8 543 end
matthiasm@8 544 case 'scaled'
matthiasm@8 545 if (opts.linewidth == -1)
matthiasm@8 546 scale = sizescale;
matthiasm@8 547 else
matthiasm@8 548 scale = opts.linewidth;
matthiasm@8 549 end
matthiasm@8 550 newlines = LocalScale(oldlines, scale, opts.linemin, opts.linemax);
matthiasm@8 551 set(allMarker,{'LineWidth'},newlines);
matthiasm@8 552 end
matthiasm@8 553 end
matthiasm@8 554
matthiasm@8 555 % adjust figure bounds to surround axes
matthiasm@8 556 if strcmp(opts.bounds,'tight')
matthiasm@8 557 if (~strncmp(opts.format,'eps',3) & LocalHas3DPlot(allAxes)) | ...
matthiasm@8 558 (strncmp(opts.format,'eps',3) & opts.separatetext)
matthiasm@8 559 if (explicitbounds == 1)
matthiasm@8 560 warning(['Cannot compute ''tight'' bounds. Using ''loose''' ...
matthiasm@8 561 ' bounds.']);
matthiasm@8 562 end
matthiasm@8 563 opts.bounds = 'loose';
matthiasm@8 564 end
matthiasm@8 565 end
matthiasm@8 566 warning('off');
matthiasm@8 567 if ~isempty(allAxes)
matthiasm@8 568 if strncmp(opts.format,'eps',3)
matthiasm@8 569 if strcmp(opts.bounds,'loose')
matthiasm@8 570 args = {args{:}, '-loose'};
matthiasm@8 571 end
matthiasm@8 572 old = LocalPushOldData(old,H,'Position', oldFigPos);
matthiasm@8 573 elseif strcmp(opts.bounds,'tight')
matthiasm@8 574 oldaunits = LocalGetAsCell(allAxes,'Units');
matthiasm@8 575 oldapos = LocalGetAsCell(allAxes,'Position');
matthiasm@8 576 oldtunits = LocalGetAsCell(allText,'units');
matthiasm@8 577 oldtpos = LocalGetAsCell(allText,'Position');
matthiasm@8 578 set(allAxes,'units','points');
matthiasm@8 579 apos = LocalGetAsCell(allAxes,'Position');
matthiasm@8 580 oldunits = get(H,'Units');
matthiasm@8 581 set(H,'units','points');
matthiasm@8 582 origfr = get(H,'position');
matthiasm@8 583 fr = [];
matthiasm@8 584 for k=1:length(allAxes)
matthiasm@8 585 if ~strcmpi(get(allAxes(k),'Tag'),'legend')
matthiasm@8 586 axesR = apos{k};
matthiasm@8 587 r = LocalAxesTightBoundingBox(axesR, allAxes(k));
matthiasm@8 588 r(1:2) = r(1:2) + axesR(1:2);
matthiasm@8 589 fr = LocalUnionRect(fr,r);
matthiasm@8 590 end
matthiasm@8 591 end
matthiasm@8 592 if isempty(fr)
matthiasm@8 593 fr = [0 0 origfr(3:4)];
matthiasm@8 594 end
matthiasm@8 595 for k=1:length(allAxes)
matthiasm@8 596 ax = allAxes(k);
matthiasm@8 597 r = apos{k};
matthiasm@8 598 r(1:2) = r(1:2) - fr(1:2);
matthiasm@8 599 set(ax,'Position',r);
matthiasm@8 600 end
matthiasm@8 601 old = LocalPushOldData(old, allAxes, {'Position'}, oldapos);
matthiasm@8 602 old = LocalPushOldData(old, allText, {'Position'}, oldtpos);
matthiasm@8 603 old = LocalPushOldData(old, allText, {'Units'}, oldtunits);
matthiasm@8 604 old = LocalPushOldData(old, allAxes, {'Units'}, oldaunits);
matthiasm@8 605 old = LocalPushOldData(old, H, 'Position', oldFigPos);
matthiasm@8 606 old = LocalPushOldData(old, H, 'Units', oldFigureUnits);
matthiasm@8 607 r = [origfr(1) origfr(2)+origfr(4)-fr(4) fr(3:4)];
matthiasm@8 608 set(H,'Position',r);
matthiasm@8 609 else
matthiasm@8 610 args = {args{:}, '-loose'};
matthiasm@8 611 old = LocalPushOldData(old,H,'Position', oldFigPos);
matthiasm@8 612 end
matthiasm@8 613 end
matthiasm@8 614
matthiasm@8 615 % Process text in a separate file if needed
matthiasm@8 616 if opts.separatetext & ~opts.applystyle
matthiasm@8 617 % First hide all text and export
matthiasm@8 618 oldtvis = LocalGetAsCell(allText,'visible');
matthiasm@8 619 set(allText,'visible','off');
matthiasm@8 620 oldax = LocalGetAsCell(allAxes,'XTickLabel',1);
matthiasm@8 621 olday = LocalGetAsCell(allAxes,'YTickLabel',1);
matthiasm@8 622 oldaz = LocalGetAsCell(allAxes,'ZTickLabel',1);
matthiasm@8 623 null = cell(length(oldax),1);
matthiasm@8 624 [null{:}] = deal([]);
matthiasm@8 625 set(allAxes,{'XTickLabel'},null);
matthiasm@8 626 set(allAxes,{'YTickLabel'},null);
matthiasm@8 627 set(allAxes,{'ZTickLabel'},null);
matthiasm@8 628 print(H, filename, args{:});
matthiasm@8 629 set(allText,{'Visible'},oldtvis);
matthiasm@8 630 set(allAxes,{'XTickLabel'},oldax);
matthiasm@8 631 set(allAxes,{'YTickLabel'},olday);
matthiasm@8 632 set(allAxes,{'ZTickLabel'},oldaz);
matthiasm@8 633 % Now hide all non-text and export as eps in painters
matthiasm@8 634 [path, name, ext] = fileparts(filename);
matthiasm@8 635 tfile = fullfile(path,[name '_t.eps']);
matthiasm@8 636 tfile2 = fullfile(path,[name '_t2.eps']);
matthiasm@8 637 foundRenderer = 0;
matthiasm@8 638 for k=1:length(args)
matthiasm@8 639 if strncmp('-d',args{k},2)
matthiasm@8 640 args{k} = '-deps';
matthiasm@8 641 elseif strncmp('-zbuffer',args{k},8) | ...
matthiasm@8 642 strncmp('-opengl', args{k},6)
matthiasm@8 643 args{k} = '-painters';
matthiasm@8 644 foundRenderer = 1;
matthiasm@8 645 end
matthiasm@8 646 end
matthiasm@8 647 if ~foundRenderer
matthiasm@8 648 args = {args{:}, '-painters'};
matthiasm@8 649 end
matthiasm@8 650 allNonText = [allLines; allLights; allPatch; ...
matthiasm@8 651 allImages; allSurf; allRect];
matthiasm@8 652 oldvis = LocalGetAsCell(allNonText,'visible');
matthiasm@8 653 oldc = LocalGetAsCell(allAxes,'color');
matthiasm@8 654 oldaxg = LocalGetAsCell(allAxes,'XGrid');
matthiasm@8 655 oldayg = LocalGetAsCell(allAxes,'YGrid');
matthiasm@8 656 oldazg = LocalGetAsCell(allAxes,'ZGrid');
matthiasm@8 657 [null{:}] = deal('off');
matthiasm@8 658 set(allAxes,{'XGrid'},null);
matthiasm@8 659 set(allAxes,{'YGrid'},null);
matthiasm@8 660 set(allAxes,{'ZGrid'},null);
matthiasm@8 661 set(allNonText,'Visible','off');
matthiasm@8 662 set(allAxes,'Color','none');
matthiasm@8 663 print(H, tfile2, args{:});
matthiasm@8 664 set(allNonText,{'Visible'},oldvis);
matthiasm@8 665 set(allAxes,{'Color'},oldc);
matthiasm@8 666 set(allAxes,{'XGrid'},oldaxg);
matthiasm@8 667 set(allAxes,{'YGrid'},oldayg);
matthiasm@8 668 set(allAxes,{'ZGrid'},oldazg);
matthiasm@8 669 %hack up the postscript file
matthiasm@8 670 fid1 = fopen(tfile,'w');
matthiasm@8 671 fid2 = fopen(tfile2,'r');
matthiasm@8 672 line = fgetl(fid2);
matthiasm@8 673 while ischar(line)
matthiasm@8 674 if strncmp(line,'%%Title',7)
matthiasm@8 675 fprintf(fid1,'%s\n',['%%Title: ', tfile]);
matthiasm@8 676 elseif (length(line) < 3)
matthiasm@8 677 fprintf(fid1,'%s\n',line);
matthiasm@8 678 elseif ~strcmp(line(end-2:end),' PR') & ...
matthiasm@8 679 ~strcmp(line(end-1:end),' L')
matthiasm@8 680 fprintf(fid1,'%s\n',line);
matthiasm@8 681 end
matthiasm@8 682 line = fgetl(fid2);
matthiasm@8 683 end
matthiasm@8 684 fclose(fid1);
matthiasm@8 685 fclose(fid2);
matthiasm@8 686 delete(tfile2);
matthiasm@8 687
matthiasm@8 688 elseif ~opts.applystyle
matthiasm@8 689 drawnow;
matthiasm@8 690 print(H, filename, args{:});
matthiasm@8 691 end
matthiasm@8 692 warning(oldwarn);
matthiasm@8 693
matthiasm@8 694 catch
matthiasm@8 695 warning(oldwarn);
matthiasm@8 696 hadError = 1;
matthiasm@8 697 end
matthiasm@8 698
matthiasm@8 699 % Restore figure settings
matthiasm@8 700 if opts.applystyle
matthiasm@8 701 varargout{1} = old;
matthiasm@8 702 else
matthiasm@8 703 for n=1:length(old.objs)
matthiasm@8 704 if ~iscell(old.values{n}) & iscell(old.prop{n})
matthiasm@8 705 old.values{n} = {old.values{n}};
matthiasm@8 706 end
matthiasm@8 707 set(old.objs{n}, old.prop{n}, old.values{n});
matthiasm@8 708 end
matthiasm@8 709 end
matthiasm@8 710
matthiasm@8 711 if hadError
matthiasm@8 712 error(deblank(lasterr));
matthiasm@8 713 end
matthiasm@8 714
matthiasm@8 715 %
matthiasm@8 716 % Local Functions
matthiasm@8 717 %
matthiasm@8 718
matthiasm@8 719 function outData = LocalPushOldData(inData, objs, prop, values)
matthiasm@8 720 outData.objs = {objs, inData.objs{:}};
matthiasm@8 721 outData.prop = {prop, inData.prop{:}};
matthiasm@8 722 outData.values = {values, inData.values{:}};
matthiasm@8 723
matthiasm@8 724 function cellArray = LocalGetAsCell(fig,prop,allowemptycell);
matthiasm@8 725 cellArray = get(fig,prop);
matthiasm@8 726 if nargin < 3
matthiasm@8 727 allowemptycell = 0;
matthiasm@8 728 end
matthiasm@8 729 if ~iscell(cellArray) & (allowemptycell | ~isempty(cellArray))
matthiasm@8 730 cellArray = {cellArray};
matthiasm@8 731 end
matthiasm@8 732
matthiasm@8 733 function newArray = LocalScale(inArray, scale, minv, maxv)
matthiasm@8 734 n = length(inArray);
matthiasm@8 735 newArray = cell(n,1);
matthiasm@8 736 for k=1:n
matthiasm@8 737 newArray{k} = min(maxv,max(minv,scale*inArray{k}(1)));
matthiasm@8 738 end
matthiasm@8 739
matthiasm@8 740 function gray = LocalMapToGray1(color)
matthiasm@8 741 gray = color;
matthiasm@8 742 if ischar(color)
matthiasm@8 743 switch color(1)
matthiasm@8 744 case 'y'
matthiasm@8 745 color = [1 1 0];
matthiasm@8 746 case 'm'
matthiasm@8 747 color = [1 0 1];
matthiasm@8 748 case 'c'
matthiasm@8 749 color = [0 1 1];
matthiasm@8 750 case 'r'
matthiasm@8 751 color = [1 0 0];
matthiasm@8 752 case 'g'
matthiasm@8 753 color = [0 1 0];
matthiasm@8 754 case 'b'
matthiasm@8 755 color = [0 0 1];
matthiasm@8 756 case 'w'
matthiasm@8 757 color = [1 1 1];
matthiasm@8 758 case 'k'
matthiasm@8 759 color = [0 0 0];
matthiasm@8 760 end
matthiasm@8 761 end
matthiasm@8 762 if ~ischar(color)
matthiasm@8 763 gray = 0.30*color(1) + 0.59*color(2) + 0.11*color(3);
matthiasm@8 764 end
matthiasm@8 765
matthiasm@8 766 function newArray = LocalMapToGray(inArray);
matthiasm@8 767 n = length(inArray);
matthiasm@8 768 newArray = cell(n,1);
matthiasm@8 769 for k=1:n
matthiasm@8 770 color = inArray{k};
matthiasm@8 771 if ~isempty(color)
matthiasm@8 772 color = LocalMapToGray1(color);
matthiasm@8 773 end
matthiasm@8 774 if isempty(color) | ischar(color)
matthiasm@8 775 newArray{k} = color;
matthiasm@8 776 else
matthiasm@8 777 newArray{k} = [color color color];
matthiasm@8 778 end
matthiasm@8 779 end
matthiasm@8 780
matthiasm@8 781 function newArray = LocalMapColorToStyle(inArray);
matthiasm@8 782 inArray = LocalGetAsCell(inArray,'Color');
matthiasm@8 783 n = length(inArray);
matthiasm@8 784 newArray = cell(n,1);
matthiasm@8 785 styles = {'-','--',':','-.'};
matthiasm@8 786 uniques = [];
matthiasm@8 787 nstyles = length(styles);
matthiasm@8 788 for k=1:n
matthiasm@8 789 gray = LocalMapToGray1(inArray{k});
matthiasm@8 790 if isempty(gray) | ischar(gray) | gray < .05
matthiasm@8 791 newArray{k} = '-';
matthiasm@8 792 else
matthiasm@8 793 if ~isempty(uniques) & any(gray == uniques)
matthiasm@8 794 ind = find(gray==uniques);
matthiasm@8 795 else
matthiasm@8 796 uniques = [uniques gray];
matthiasm@8 797 ind = length(uniques);
matthiasm@8 798 end
matthiasm@8 799 newArray{k} = styles{mod(ind-1,nstyles)+1};
matthiasm@8 800 end
matthiasm@8 801 end
matthiasm@8 802
matthiasm@8 803 function newArray = LocalMapCData(inArray);
matthiasm@8 804 n = length(inArray);
matthiasm@8 805 newArray = cell(n,1);
matthiasm@8 806 for k=1:n
matthiasm@8 807 color = inArray{k};
matthiasm@8 808 if (ndims(color) == 3) & isa(color,'double')
matthiasm@8 809 gray = 0.30*color(:,:,1) + 0.59*color(:,:,2) + 0.11*color(:,:,3);
matthiasm@8 810 color(:,:,1) = gray;
matthiasm@8 811 color(:,:,2) = gray;
matthiasm@8 812 color(:,:,3) = gray;
matthiasm@8 813 end
matthiasm@8 814 newArray{k} = color;
matthiasm@8 815 end
matthiasm@8 816
matthiasm@8 817 function outData = LocalUpdateColors(inArray, prop, inData)
matthiasm@8 818 value = LocalGetAsCell(inArray,prop);
matthiasm@8 819 outData.objs = {inData.objs{:}, inArray};
matthiasm@8 820 outData.prop = {inData.prop{:}, {prop}};
matthiasm@8 821 outData.values = {inData.values{:}, value};
matthiasm@8 822 if (~isempty(value))
matthiasm@8 823 if strcmp(prop,'CData')
matthiasm@8 824 value = LocalMapCData(value);
matthiasm@8 825 else
matthiasm@8 826 value = LocalMapToGray(value);
matthiasm@8 827 end
matthiasm@8 828 set(inArray,{prop},value);
matthiasm@8 829 end
matthiasm@8 830
matthiasm@8 831 function bool = LocalIsPositiveScalar(value)
matthiasm@8 832 bool = isnumeric(value) & ...
matthiasm@8 833 prod(size(value)) == 1 & ...
matthiasm@8 834 value > 0;
matthiasm@8 835
matthiasm@8 836 function value = LocalToNum(value,auto)
matthiasm@8 837 if ischar(value)
matthiasm@8 838 if strcmp(value,'auto')
matthiasm@8 839 value = auto;
matthiasm@8 840 else
matthiasm@8 841 value = str2num(value);
matthiasm@8 842 end
matthiasm@8 843 end
matthiasm@8 844
matthiasm@8 845 %convert a struct to {field1,val1,field2,val2,...}
matthiasm@8 846 function c = LocalToCell(s)
matthiasm@8 847 f = fieldnames(s);
matthiasm@8 848 v = struct2cell(s);
matthiasm@8 849 opts = cell(2,length(f));
matthiasm@8 850 opts(1,:) = f;
matthiasm@8 851 opts(2,:) = v;
matthiasm@8 852 c = {opts{:}};
matthiasm@8 853
matthiasm@8 854 function c = LocalIsHG(obj,hgtype)
matthiasm@8 855 c = 0;
matthiasm@8 856 if (length(obj) == 1) & ishandle(obj)
matthiasm@8 857 c = strcmp(get(obj,'type'),hgtype);
matthiasm@8 858 end
matthiasm@8 859
matthiasm@8 860 function c = LocalHas3DPlot(a)
matthiasm@8 861 zticks = LocalGetAsCell(a,'ZTickLabel');
matthiasm@8 862 c = 0;
matthiasm@8 863 for k=1:length(zticks)
matthiasm@8 864 if ~isempty(zticks{k})
matthiasm@8 865 c = 1;
matthiasm@8 866 return;
matthiasm@8 867 end
matthiasm@8 868 end
matthiasm@8 869
matthiasm@8 870 function r = LocalUnionRect(r1,r2)
matthiasm@8 871 if isempty(r1)
matthiasm@8 872 r = r2;
matthiasm@8 873 elseif isempty(r2)
matthiasm@8 874 r = r1;
matthiasm@8 875 elseif max(r2(3:4)) > 0
matthiasm@8 876 left = min(r1(1),r2(1));
matthiasm@8 877 bot = min(r1(2),r2(2));
matthiasm@8 878 right = max(r1(1)+r1(3),r2(1)+r2(3));
matthiasm@8 879 top = max(r1(2)+r1(4),r2(2)+r2(4));
matthiasm@8 880 r = [left bot right-left top-bot];
matthiasm@8 881 else
matthiasm@8 882 r = r1;
matthiasm@8 883 end
matthiasm@8 884
matthiasm@8 885 function c = LocalLabelsMatchTicks(labs,ticks)
matthiasm@8 886 c = 0;
matthiasm@8 887 try
matthiasm@8 888 t1 = num2str(ticks(1));
matthiasm@8 889 n = length(ticks);
matthiasm@8 890 tend = num2str(ticks(n));
matthiasm@8 891 c = strncmp(labs(1),t1,length(labs(1))) & ...
matthiasm@8 892 strncmp(labs(n),tend,length(labs(n)));
matthiasm@8 893 end
matthiasm@8 894
matthiasm@8 895 function r = LocalAxesTightBoundingBox(axesR, a)
matthiasm@8 896 r = [];
matthiasm@8 897 atext = findall(a,'type','text','visible','on');
matthiasm@8 898 if ~isempty(atext)
matthiasm@8 899 set(atext,'units','points');
matthiasm@8 900 res=LocalGetAsCell(atext,'extent');
matthiasm@8 901 for n=1:length(atext)
matthiasm@8 902 r = LocalUnionRect(r,res{n});
matthiasm@8 903 end
matthiasm@8 904 end
matthiasm@8 905 if strcmp(get(a,'visible'),'on')
matthiasm@8 906 r = LocalUnionRect(r,[0 0 axesR(3:4)]);
matthiasm@8 907 oldunits = get(a,'fontunits');
matthiasm@8 908 set(a,'fontunits','points');
matthiasm@8 909 label = text(0,0,'','parent',a,...
matthiasm@8 910 'units','points',...
matthiasm@8 911 'fontsize',get(a,'fontsize'),...
matthiasm@8 912 'fontname',get(a,'fontname'),...
matthiasm@8 913 'fontweight',get(a,'fontweight'),...
matthiasm@8 914 'fontangle',get(a,'fontangle'),...
matthiasm@8 915 'visible','off');
matthiasm@8 916 fs = get(a,'fontsize');
matthiasm@8 917
matthiasm@8 918 % handle y axis tick labels
matthiasm@8 919 ry = [0 -fs/2 0 axesR(4)+fs];
matthiasm@8 920 ylabs = get(a,'yticklabels');
matthiasm@8 921 yticks = get(a,'ytick');
matthiasm@8 922 maxw = 0;
matthiasm@8 923 if ~isempty(ylabs)
matthiasm@8 924 for n=1:size(ylabs,1)
matthiasm@8 925 set(label,'string',ylabs(n,:));
matthiasm@8 926 ext = get(label,'extent');
matthiasm@8 927 maxw = max(maxw,ext(3));
matthiasm@8 928 end
matthiasm@8 929 if ~LocalLabelsMatchTicks(ylabs,yticks) & ...
matthiasm@8 930 strcmp(get(a,'xaxislocation'),'bottom')
matthiasm@8 931 ry(4) = ry(4) + 1.5*ext(4);
matthiasm@8 932 end
matthiasm@8 933 if strcmp(get(a,'yaxislocation'),'left')
matthiasm@8 934 ry(1) = -(maxw+5);
matthiasm@8 935 else
matthiasm@8 936 ry(1) = axesR(3);
matthiasm@8 937 end
matthiasm@8 938 ry(3) = maxw+5;
matthiasm@8 939 r = LocalUnionRect(r,ry);
matthiasm@8 940 end
matthiasm@8 941
matthiasm@8 942 % handle x axis tick labels
matthiasm@8 943 rx = [0 0 0 fs+5];
matthiasm@8 944 xlabs = get(a,'xticklabels');
matthiasm@8 945 xticks = get(a,'xtick');
matthiasm@8 946 if ~isempty(xlabs)
matthiasm@8 947 if strcmp(get(a,'xaxislocation'),'bottom')
matthiasm@8 948 rx(2) = -(fs+5);
matthiasm@8 949 if ~LocalLabelsMatchTicks(xlabs,xticks);
matthiasm@8 950 rx(4) = rx(4) + 2*fs;
matthiasm@8 951 rx(2) = rx(2) - 2*fs;
matthiasm@8 952 end
matthiasm@8 953 else
matthiasm@8 954 rx(2) = axesR(4);
matthiasm@8 955 % exponent is still below axes
matthiasm@8 956 if ~LocalLabelsMatchTicks(xlabs,xticks);
matthiasm@8 957 rx(4) = rx(4) + axesR(4) + 2*fs;
matthiasm@8 958 rx(2) = -2*fs;
matthiasm@8 959 end
matthiasm@8 960 end
matthiasm@8 961 set(label,'string',xlabs(1,:));
matthiasm@8 962 ext1 = get(label,'extent');
matthiasm@8 963 rx(1) = -ext1(3)/2;
matthiasm@8 964 set(label,'string',xlabs(size(xlabs,1),:));
matthiasm@8 965 ext2 = get(label,'extent');
matthiasm@8 966 rx(3) = axesR(3) + (ext2(3) + ext1(3))/2;
matthiasm@8 967 r = LocalUnionRect(r,rx);
matthiasm@8 968 end
matthiasm@8 969 set(a,'fontunits',oldunits);
matthiasm@8 970 delete(label);
matthiasm@8 971 end
matthiasm@8 972
matthiasm@8 973 function c = LocalManualAxesMode(old, allAxes, base)
matthiasm@8 974 xs = ['X' base];
matthiasm@8 975 ys = ['Y' base];
matthiasm@8 976 zs = ['Z' base];
matthiasm@8 977 oldXMode = LocalGetAsCell(allAxes,xs);
matthiasm@8 978 oldYMode = LocalGetAsCell(allAxes,ys);
matthiasm@8 979 oldZMode = LocalGetAsCell(allAxes,zs);
matthiasm@8 980 old = LocalPushOldData(old, allAxes, {xs}, oldXMode);
matthiasm@8 981 old = LocalPushOldData(old, allAxes, {ys}, oldYMode);
matthiasm@8 982 old = LocalPushOldData(old, allAxes, {zs}, oldZMode);
matthiasm@8 983 set(allAxes,xs,'manual');
matthiasm@8 984 set(allAxes,ys,'manual');
matthiasm@8 985 set(allAxes,zs,'manual');
matthiasm@8 986 c = old;
matthiasm@8 987
matthiasm@8 988 function val = LocalCheckAuto(val, auto)
matthiasm@8 989 if ischar(val) & strcmp(val,'auto')
matthiasm@8 990 val = auto;
matthiasm@8 991 end