wolffd@0
|
1 function out = centeraxes(ax,opt)
|
wolffd@0
|
2 % out = centeraxes(ax,opt)
|
wolffd@0
|
3 %==========================================================================
|
wolffd@0
|
4 % Center the coordinate axes of a plot so that they pass through the
|
wolffd@0
|
5 % origin.
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % Input: 0, 1 or 2 arguments
|
wolffd@0
|
8 % 0: Moves the coordinate axes of 'gca', i.e. the currently active
|
wolffd@0
|
9 % axes.
|
wolffd@0
|
10 % 1: ax, a handle to the axes which should be moved.
|
wolffd@0
|
11 % 2: ax, opt, where opt is a struct with further options
|
wolffd@0
|
12 % opt.fontsize = 'size of axis tick labels'
|
wolffd@0
|
13 % opt.fontname = name of axis tick font.
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % If the opt struct is ommitted, the current values for the axes are used.
|
wolffd@0
|
16 %
|
wolffd@0
|
17 % Output: stuct out containing handles to all line objects created by this
|
wolffd@0
|
18 % function.
|
wolffd@0
|
19 %
|
wolffd@0
|
20 %==========================================================================
|
wolffd@0
|
21 % Version: 1.1
|
wolffd@0
|
22 % Created: October 1, 2008, by Johan E. Carlson
|
wolffd@0
|
23 % Last modified: November 21, 2009, by Johan E. Carlson
|
wolffd@0
|
24 %==========================================================================
|
wolffd@0
|
25
|
wolffd@0
|
26 if nargin < 2,
|
wolffd@0
|
27 fontsize = get(ax,'FontSize');
|
wolffd@0
|
28 fontname = get(ax,'FontName');
|
wolffd@0
|
29 end
|
wolffd@0
|
30 if nargin < 1,
|
wolffd@0
|
31 ax = gca;
|
wolffd@0
|
32 end
|
wolffd@0
|
33
|
wolffd@0
|
34 if nargin == 2,
|
wolffd@0
|
35 if isfield(opt,'fontsize'),
|
wolffd@0
|
36 fontsize = opt.fontsize;
|
wolffd@0
|
37 else
|
wolffd@0
|
38 fontsize = get(ax,'FontSize');
|
wolffd@0
|
39 end;
|
wolffd@0
|
40 if isfield(opt,'fontname'),
|
wolffd@0
|
41 fontname = opt.fontname;
|
wolffd@0
|
42 else
|
wolffd@0
|
43 fontname = get(ax,'FontName');
|
wolffd@0
|
44 end
|
wolffd@0
|
45 end
|
wolffd@0
|
46
|
wolffd@0
|
47 axes(ax);
|
wolffd@0
|
48 set(gcf,'color',[1 1 1]);
|
wolffd@0
|
49 xtext = get(get(ax,'xlabel'),'string');
|
wolffd@0
|
50 ytext = get(get(ax,'ylabel'),'string');
|
wolffd@0
|
51
|
wolffd@0
|
52 %--------------------------------------------------------------------------
|
wolffd@0
|
53 % Check if the current coordinate system include the origin. If not, change
|
wolffd@0
|
54 % it so that it does!
|
wolffd@0
|
55 %--------------------------------------------------------------------------
|
wolffd@0
|
56 xlim = 1.1*get(ax,'xlim');
|
wolffd@0
|
57 ylim = 1.1*get(ax,'ylim');
|
wolffd@0
|
58 set(ax,'xlim',xlim);
|
wolffd@0
|
59 set(ax,'ylim',ylim);
|
wolffd@0
|
60
|
wolffd@0
|
61 if xlim(1)>0,
|
wolffd@0
|
62 xlim(1) = 0;
|
wolffd@0
|
63 end
|
wolffd@0
|
64
|
wolffd@0
|
65 if ylim(1)>0,
|
wolffd@0
|
66 ylim(1) = 0;
|
wolffd@0
|
67 end
|
wolffd@0
|
68
|
wolffd@0
|
69 if xlim(2) < 0,
|
wolffd@0
|
70 xlim(2) = 0;
|
wolffd@0
|
71 end
|
wolffd@0
|
72
|
wolffd@0
|
73 if ylim(2) < 0,
|
wolffd@0
|
74 ylim(2) = 0;
|
wolffd@0
|
75 end;
|
wolffd@0
|
76
|
wolffd@0
|
77 set(ax,'xlim',xlim,'ylim',ylim);
|
wolffd@0
|
78
|
wolffd@0
|
79
|
wolffd@0
|
80 % -------------------------------------------------------------------------
|
wolffd@0
|
81 % Caculate size of the "axis tick marks"
|
wolffd@0
|
82 % -------------------------------------------------------------------------
|
wolffd@0
|
83 axpos = get(ax,'position');
|
wolffd@0
|
84 figpos = get(gcf,'position');
|
wolffd@0
|
85 aspectratio = axpos(4) / (axpos(3));
|
wolffd@0
|
86 xsize = xlim(2) - xlim(1);
|
wolffd@0
|
87 ysize = ylim(2) - ylim(1);
|
wolffd@0
|
88 xticksize = ysize/figpos(4)*12;
|
wolffd@0
|
89 yticksize = xsize*aspectratio/figpos(3)*12;
|
wolffd@0
|
90
|
wolffd@0
|
91 % -------------------------------------------------------------------------
|
wolffd@0
|
92 % Store old tick values and tick labels
|
wolffd@0
|
93 % -------------------------------------------------------------------------
|
wolffd@0
|
94 ytick = get(ax,'YTick');
|
wolffd@0
|
95 xtick = get(ax,'XTick');
|
wolffd@0
|
96 xticklab = get(ax,'XTickLabel');
|
wolffd@0
|
97 yticklab = get(ax,'YTickLabel');
|
wolffd@0
|
98
|
wolffd@0
|
99 % -------------------------------------------------------------------------
|
wolffd@0
|
100 % Draw new coordinate system
|
wolffd@0
|
101 % -------------------------------------------------------------------------
|
wolffd@0
|
102
|
wolffd@0
|
103 yax = line([0; 0],[ylim(1)-1; ylim(2)]);
|
wolffd@0
|
104 xax = line([xlim(1); xlim(2)],[0; 0]);
|
wolffd@0
|
105 set(xax,'color',[0 0 0])
|
wolffd@0
|
106 set(yax,'color',[0 0 0])
|
wolffd@0
|
107
|
wolffd@0
|
108 % Draw x-axis ticks
|
wolffd@0
|
109 for k = 1:length(xtick),
|
wolffd@0
|
110 newxtick(k) = line([xtick(k); xtick(k)],[-xticksize/2; xticksize/2]);
|
wolffd@0
|
111 if (xtick(k)~=0),
|
wolffd@0
|
112 newxticklab(k) = text(xtick(k),-1.5*xticksize, strtrim(xticklab(k,:)));
|
wolffd@0
|
113 set(newxticklab(k),'HorizontalAlignment','center',...
|
wolffd@0
|
114 'Fontsize',fontsize,'FontName',fontname);
|
wolffd@0
|
115 end
|
wolffd@0
|
116 end
|
wolffd@0
|
117 set(newxtick,'color',[0 0 0]);
|
wolffd@0
|
118
|
wolffd@0
|
119 % Draw y-axis ticks
|
wolffd@0
|
120 for k = 1:length(ytick),
|
wolffd@0
|
121 newytick(k) = line([-yticksize/2; yticksize/2],[ytick(k); ytick(k)]);
|
wolffd@0
|
122 if (ytick(k)~=0),
|
wolffd@0
|
123 newyticklab(k) = text(-.8*yticksize,ytick(k), yticklab(k,:));
|
wolffd@0
|
124 set(newyticklab(k),'HorizontalAlignment','right',...
|
wolffd@0
|
125 'FontSize',fontsize,'FontName',fontname);
|
wolffd@0
|
126 end
|
wolffd@0
|
127 end
|
wolffd@0
|
128 set(newytick,'color',[0 0 0]);
|
wolffd@0
|
129
|
wolffd@0
|
130 %--------------------------------------------------------------------------
|
wolffd@0
|
131 % Move xlabels
|
wolffd@0
|
132 %--------------------------------------------------------------------------
|
wolffd@0
|
133 newxlabel = text(xlim(2),-1.5*xticksize,xtext);
|
wolffd@0
|
134 set(newxlabel,'HorizontalAlignment','center',...
|
wolffd@0
|
135 'FontWeight','demi','FontSize',fontsize+2,'FontName',fontname);
|
wolffd@0
|
136
|
wolffd@0
|
137 newylabel = text(-yticksize,ylim(2)*1.02,ytext);
|
wolffd@0
|
138 set(newylabel,'HorizontalAlignment','right','VerticalAlignment','top',...
|
wolffd@0
|
139 'FontWeight','demi','FontSize',fontsize+2,'FontName',fontname);
|
wolffd@0
|
140
|
wolffd@0
|
141 %--------------------------------------------------------------------------
|
wolffd@0
|
142 % Create arrowheads
|
wolffd@0
|
143 %--------------------------------------------------------------------------
|
wolffd@0
|
144 x = [0; -yticksize/4; yticksize/4];
|
wolffd@0
|
145 y = [ylim(2); ylim(2)-xticksize; ylim(2)-xticksize];
|
wolffd@0
|
146 patch(x,y,[0 0 0])
|
wolffd@0
|
147
|
wolffd@0
|
148 x = [xlim(2); xlim(2)-yticksize; xlim(2)-yticksize];
|
wolffd@0
|
149 y = [0; xticksize/4; -xticksize/4];
|
wolffd@0
|
150 patch(x,y,[0 0 0])
|
wolffd@0
|
151
|
wolffd@0
|
152 axis off;
|
wolffd@0
|
153 box off;
|
wolffd@0
|
154
|
wolffd@0
|
155 %--------------------------------------------------------------------------
|
wolffd@0
|
156 % Create output struct
|
wolffd@0
|
157 %--------------------------------------------------------------------------
|
wolffd@0
|
158
|
wolffd@0
|
159 if nargout > 0,
|
wolffd@0
|
160 out.xaxis = xax;
|
wolffd@0
|
161 out.yaxis = yax;
|
wolffd@0
|
162 out.xtick = newxtick;
|
wolffd@0
|
163 out.ytick = newytick;
|
wolffd@0
|
164 out.xticklabel = newxticklab;
|
wolffd@0
|
165 out.yticklabel = newyticklab;
|
wolffd@0
|
166 out.newxlabel = newxlabel;
|
wolffd@0
|
167 out.newylabel = newylabel;
|
wolffd@0
|
168 end; |