-
Notifications
You must be signed in to change notification settings - Fork 1
/
fix_xticklabels.m
163 lines (134 loc) · 3.77 KB
/
fix_xticklabels.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
function ht = fix_xticklabels(handle,margins,textopts)
%FIX_XTICKLABELS will determine maximum allowed width
% of long XTickLabels and convert them into multiple line
% format to fit maximum allowed width
%
%
% IN:
% handle [optional] - axes handle (defaults to gca)
% margins [optional] - relative margins width (defaults to 0.1)
% texopts [optional] - cell array of addtitional text formating
% options (defaults to {} )
%
% OUT:
% ht - column vector of handles for created text objects
%
% This code was derrived from MY_XTICKLABELS by Pekka Kumpulainen
% http://www.mathworks.com/matlabcentral/fileexchange/19059-myxticklabels
% EXAMPLE:
% figure;
%
% bar(rand(1,4));
% set(gca,'XTickLabel',{'Really very very long text', ...
% 'One more long long long text', ...
% 'Short one','Long long long text again'});
% fix_xticklabels();
%
% figure;
%
% bar(rand(1,4));
% set(gca,'XTickLabel',{'Really very very long text', ...
% 'One more long long long text', ...
% 'Short one','Long long long text again'});
% fix_xticklabels(gca,0.1,{'FontSize',16});
%
% Mikhail Erofeev 07.06.2013
% Pekka Kumpulainen 12.2.2008
if(~exist('handle','var'))
handle = gca;
end
if(~exist('textopts','var'))
textopts={};
end
if(~exist('margins','var'))
margins = 0.1;
end
xtickpos = get(handle,'XTick');
xtickstring = get(handle,'XTickLabel');
set(handle, 'XTickLabel','')
h_olds = findobj(handle, 'Tag', 'MUXTL');
if ~isempty(h_olds)
delete(h_olds)
end
%% Make XTickLabels
NTick = length(xtickpos);
Ybot = min(get(handle,'YLim'));
Xbot = get(handle,'XLim');
max_w = ((max(Xbot) - min(Xbot) )/NTick)*(1-margins);
ht = zeros(NTick,1);
NLabels = length(xtickstring);
for ii = 1:NTick
str_id = mod( ii - 1 , NLabels) + 1;
ht(ii) = text('String',xtickstring{str_id}, ...
'Units','data', ...
'VerticalAlignment', 'top', ...
'HorizontalAlignment', 'center ', ...
'Position',[xtickpos(ii) Ybot], ...
'Tag','MUXTL');
end
if ~isempty(textopts)
set(ht,textopts{:})
end
for ii = 1:NTick
auto_width(ht(ii),max_w);
end
%% squeeze axis if needed
set(handle,'Units','pixels')
Axpos = get(handle,'Position');
% set(Hfig,'Units','pixels')
% Figpos = get(Hfig,'Position');
set(ht,'Units','pixels')
TickExt = zeros(NTick,4);
for ii = 1:NTick
TickExt(ii,:) = get(ht(ii),'Extent');
end
xlabh = get(handle,'XLabel');
minbottom = min(TickExt(:,2));
if( ~isempty(xlabh) )
oldu = get(xlabh,'Units');
set(xlabh,'Units','pixels');
ext = get(xlabh,'Extent');
pos = get(xlabh,'Position');
pos(2) = minbottom - ext(4);
set(xlabh,'Position',pos);
ext = get(xlabh,'Extent');
minbottom = ext(2);
set(xlabh,'Units',oldu)
end
needmove = -(Axpos(2) + minbottom);
if needmove>0;
Axpos(2) = Axpos(2)+needmove+2;
Axpos(4) = Axpos(4)-needmove+2;
set(handle,'Position',Axpos);
end
set(handle,'Units','normalized')
set(ht,'Units','normalized')
end
function []=auto_width(h,maxw)
str = get(h,'string');
set(h,'string',{});
words = regexp(str,' ','split');
tail = words(2:end);
res = words(1);
ln = 1;
while(~isempty(tail))
lin = res{ln};
lin_e = [lin ' ' tail{1}];
set(h,'string',lin_e);
ext = get(h,'Extent');
width=ext(3);
if(width>maxw)
ln =ln+1;
res{ln} = tail{1};
else
res{ln} = lin_e;
end
tail = tail(2:end);
end
set(h,'string',res);
ext = get(h,'Extent');
width=ext(3);
if(width>maxw)
warning('FIX_XTICKLABELS:over','Overflown box');
end
end