Daniel@0: function ax = axis_pct(pct) Daniel@0: % AXIS_PCT Set reasonable axis limits. Daniel@0: % AXIS_PCT(pct) sets axis limits to extend pct% beyond limits of plotted Daniel@0: % objects. Default is 5%. Daniel@0: % Works for linear or log scale. Daniel@0: % Unfortunately, the axes won't change when new points are plotted. Daniel@0: Daniel@0: if nargin < 1 Daniel@0: pct = 0.05; Daniel@0: end Daniel@0: ax = [Inf -Inf Inf -Inf Inf -Inf]; Daniel@0: Daniel@0: % find bounding box of plotted objects Daniel@0: children = get(gca,'children'); Daniel@0: for child = children' Daniel@0: if strcmp(get(child,'type'),'text') Daniel@0: xyz = get(child,'position'); Daniel@0: % need to determine bounding box of the text Daniel@0: c([1 2]) = xyz(1); Daniel@0: c([3 4]) = xyz(2); Daniel@0: c([5 6]) = xyz(3); Daniel@0: else Daniel@0: x = get(child,'xdata'); Daniel@0: c(1) = min(x); Daniel@0: c(2) = max(x); Daniel@0: y = get(child,'ydata'); Daniel@0: c(3) = min(y); Daniel@0: c(4) = max(y); Daniel@0: z = get(child,'zdata'); Daniel@0: if isempty(z) Daniel@0: c([5 6]) = 0; Daniel@0: else Daniel@0: c(5) = min(z); Daniel@0: c(6) = max(z); Daniel@0: end Daniel@0: end Daniel@0: ax([1 3 5]) = min(ax([1 3 5]), c([1 3 5])); Daniel@0: ax([2 4 6]) = max(ax([2 4 6]), c([2 4 6])); Daniel@0: end Daniel@0: if strcmp(get(gca,'xscale'), 'log') Daniel@0: ax([1 2]) = log(ax([1 2])); Daniel@0: end Daniel@0: if strcmp(get(gca,'yscale'), 'log') Daniel@0: ax([3 4]) = log(ax([3 4])); Daniel@0: end Daniel@0: dx = ax(2)-ax(1); Daniel@0: if dx == 0 Daniel@0: dx = 1; Daniel@0: end Daniel@0: dy = ax(4)-ax(3); Daniel@0: if dy == 0 Daniel@0: dy = 1; Daniel@0: end Daniel@0: dz = ax(6)-ax(5); Daniel@0: if dz == 0 Daniel@0: dz = 1; Daniel@0: end Daniel@0: ax = ax + [-dx dx -dy dy -dz dz]*pct; Daniel@0: if strcmp(get(gca,'xscale'), 'log') Daniel@0: ax([1 2]) = exp(ax([1 2])); Daniel@0: end Daniel@0: if strcmp(get(gca,'yscale'), 'log') Daniel@0: ax([3 4]) = exp(ax([3 4])); Daniel@0: end Daniel@0: % clip for 2D Daniel@0: ax = ax(1:length(axis)); Daniel@0: axis(ax); Daniel@0: if nargout < 1 Daniel@0: clear ax Daniel@0: end