wolffd@0
|
1 function ax = axis_pct(pct)
|
wolffd@0
|
2 % AXIS_PCT Set reasonable axis limits.
|
wolffd@0
|
3 % AXIS_PCT(pct) sets axis limits to extend pct% beyond limits of plotted
|
wolffd@0
|
4 % objects. Default is 5%.
|
wolffd@0
|
5 % Works for linear or log scale.
|
wolffd@0
|
6 % Unfortunately, the axes won't change when new points are plotted.
|
wolffd@0
|
7
|
wolffd@0
|
8 if nargin < 1
|
wolffd@0
|
9 pct = 0.05;
|
wolffd@0
|
10 end
|
wolffd@0
|
11 ax = [Inf -Inf Inf -Inf Inf -Inf];
|
wolffd@0
|
12
|
wolffd@0
|
13 % find bounding box of plotted objects
|
wolffd@0
|
14 children = get(gca,'children');
|
wolffd@0
|
15 for child = children'
|
wolffd@0
|
16 if strcmp(get(child,'type'),'text')
|
wolffd@0
|
17 xyz = get(child,'position');
|
wolffd@0
|
18 % need to determine bounding box of the text
|
wolffd@0
|
19 c([1 2]) = xyz(1);
|
wolffd@0
|
20 c([3 4]) = xyz(2);
|
wolffd@0
|
21 c([5 6]) = xyz(3);
|
wolffd@0
|
22 else
|
wolffd@0
|
23 x = get(child,'xdata');
|
wolffd@0
|
24 c(1) = min(x);
|
wolffd@0
|
25 c(2) = max(x);
|
wolffd@0
|
26 y = get(child,'ydata');
|
wolffd@0
|
27 c(3) = min(y);
|
wolffd@0
|
28 c(4) = max(y);
|
wolffd@0
|
29 z = get(child,'zdata');
|
wolffd@0
|
30 if isempty(z)
|
wolffd@0
|
31 c([5 6]) = 0;
|
wolffd@0
|
32 else
|
wolffd@0
|
33 c(5) = min(z);
|
wolffd@0
|
34 c(6) = max(z);
|
wolffd@0
|
35 end
|
wolffd@0
|
36 end
|
wolffd@0
|
37 ax([1 3 5]) = min(ax([1 3 5]), c([1 3 5]));
|
wolffd@0
|
38 ax([2 4 6]) = max(ax([2 4 6]), c([2 4 6]));
|
wolffd@0
|
39 end
|
wolffd@0
|
40 if strcmp(get(gca,'xscale'), 'log')
|
wolffd@0
|
41 ax([1 2]) = log(ax([1 2]));
|
wolffd@0
|
42 end
|
wolffd@0
|
43 if strcmp(get(gca,'yscale'), 'log')
|
wolffd@0
|
44 ax([3 4]) = log(ax([3 4]));
|
wolffd@0
|
45 end
|
wolffd@0
|
46 dx = ax(2)-ax(1);
|
wolffd@0
|
47 if dx == 0
|
wolffd@0
|
48 dx = 1;
|
wolffd@0
|
49 end
|
wolffd@0
|
50 dy = ax(4)-ax(3);
|
wolffd@0
|
51 if dy == 0
|
wolffd@0
|
52 dy = 1;
|
wolffd@0
|
53 end
|
wolffd@0
|
54 dz = ax(6)-ax(5);
|
wolffd@0
|
55 if dz == 0
|
wolffd@0
|
56 dz = 1;
|
wolffd@0
|
57 end
|
wolffd@0
|
58 ax = ax + [-dx dx -dy dy -dz dz]*pct;
|
wolffd@0
|
59 if strcmp(get(gca,'xscale'), 'log')
|
wolffd@0
|
60 ax([1 2]) = exp(ax([1 2]));
|
wolffd@0
|
61 end
|
wolffd@0
|
62 if strcmp(get(gca,'yscale'), 'log')
|
wolffd@0
|
63 ax([3 4]) = exp(ax([3 4]));
|
wolffd@0
|
64 end
|
wolffd@0
|
65 % clip for 2D
|
wolffd@0
|
66 ax = ax(1:length(axis));
|
wolffd@0
|
67 axis(ax);
|
wolffd@0
|
68 if nargout < 1
|
wolffd@0
|
69 clear ax
|
wolffd@0
|
70 end
|