forked from mdqyy/MATLAB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_ticks.m
562 lines (508 loc) · 17.5 KB
/
format_ticks.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BEGIN HEADER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Name: format_tick.m
%
%Usage: [hx,hy] = ...
% format_tick(h,tickx,ticky,tickposx,tickposy,rotx,roty,offset,...
% varargin);
%
%Description: Replace or appends XTickLabels and YTickLabels of axis handle
% h with input tickx and ticky array
%
%***NOTE!***: BE SURE TO DELETE ANY PREVIOUS TEXT OBJECTS CREATED BY THIS
% FUNCTION BEFORE RUNNING THIS ON THE SAME FIGURE TWICE
%
%Required Inputs:
% h : handle of axis to change tick labels (can use gca)
% tickx : cell array of tick labels or string to append to current
% labels
% (Defaults to appending degree symbols if not input)
%
%Optional Inputs
% ticky : cell array of tick labels or string to append to current
% labels (Can use [] or not specify to ignore)
% tickposx : Vector of x positions where you want the tick labels
% (Can use [] or not specify to ignore)
% tickposy : Vector of y positions where you want the tick labels
% (Can use [] or not specify to ignore)
% rotx : Number of degrees to rotate x tick labels
% (Can use [] or not specify to ignore) Default = 0.0
% roty : Number of degrees to rotate y tick labels
% (Can use [] or not specify to ignore) Default = 0.0
% offset : Label offsets from axis in fraction of total range
% (Can use [] or not specify to ignore) Default = 0.0
%
%Optional Inputs:%
% Any standard text formatting parameters such as
% 'FontSize','FontWeight',etc.
% Use the same way you would in a set command after putting
% in the required input values.
%
%Outputs:
% hx: handle of text objects created for XTickLabels
% hy: handle of text objects created for YTickLabels
%
%Function Calls:
% None
%
%Required Data Files:
% None
%
%
%Example:
% ;Example 1: Append Degree Symbols to X-Axis of a Plot
% figure;
% plot(1:10,1:10);
% [hx,hy] = format_ticks(gca);
%
% ;Example 2: Append Degree Symbolts to X and Y Axes of a Plot
% figure;
% plot(1:10,1:10);
% [hx,hy] = format_ticks(gca,'^{\circ}','^{\circ}');
%
% ;Example 2: Append Degree Symbolts to X and Y Axes of a Plot and
% ; put a 45 degree tilt on them
% figure;
% plot(1:10,1:10);
% [hx,hy] = format_ticks(gca,'^{\circ}','^{\circ}',[],[],45,45);
%
% ;Example 3: Make a plot with fractions on the x tick labels
% figure
% plot(1:10,1:10);
% [hx,hy] = format_ticks(gca,{'$1$','$2\frac{1}{2}$','$9\frac{1}{2}$'},...
% [],[1,2.5,9.5]);
%
% ;Example 4: Make a plot with degrees on y tick label and fractions
% ; on x
% figure
% plot(0:10,0:10);
% [hx,hy] = format_ticks(gca,...
% {'$0$','$2\frac{1}{2}$','$5$','$7\frac{1}{2}$','$10$'},...
% '$^{\circ}$',[0,2.5,5,7.5,10],[],0,45,[],...
% 'FontSize',16,'FontWeight','Bold');
%
%Change Log:
% 08/19/2007: Origin Version Created by Alex Hayes
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BEGIN FUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [hx,hy] = ...
format_ticks(h,tickx,ticky,tickposx,tickposy,rotx,roty,offset,varargin)
%define axis text offset (percentage of total range)
if ~exist('offset','var');
offset = 0.02;
elseif length(offset) == 0;
offset = 0.02;
end;
%make sure the axis handle input really exists
if ~exist('h','var');
h = gca;
warning(['Axis handle NOT Input, Defaulting to Current Axes, '...
num2str(h)]);
elseif length(h) == 0;
h = gca;
warning(['Axis Handle NOT Input, Defaulting to Current Axes, '...
num2str(h)]);
elseif ~ishandle(h(1))
warning(['Input (' num2str(h(1)) ') is NOT an axis handle, ' ...
'defaulting to current axis, ' num2str(h)]);
h = gca;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%BEGIN: FIRST THE X-AXIS TICK LABELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%fix the XTickLabels if they have been erased in the past
if length(get(h,'XTickLabel'))==0;
set(h,'XTickLabel',get(h,'XTick'));
end;
%set the xtick positions if entered
if exist('tickposx','var');
if length(tickposx) > 0;
set(h,'XTick',tickposx);
end;
tickposx = get(h,'XTick');
set(h,'XTickLabel',tickposx);
end;
%make sure the xtick positions are in the xlimit range
if exist('tickposx','var');
if length(tickposx) > 0;
lim = get(h,'XLim');
if lim(1) > min(tickposx);
lim(1) = min(tickposx);
end;
if lim(2) < max(tickposx);
lim(2) = max(tickposx);
end;
set(h,'XLim',lim);
end;
end;
%get the tick labels and positions if the user did not input them
if ~exist('tickx','var');
tickx = get(h,'XTickLabel');
if ischar(tickx);
temp = tickx;
tickx = cell(1,size(temp,1));
for j=1:size(temp,1);
tickx{j} = strtrim( temp(j,:) );
end;
end;
append = '^{\circ}';
for j=1:length(tickx);
tickx{j} = [tickx{j} append];
end;
elseif length(tickx) == 0;
tickx = get(h,'XTickLabel');
if ischar(tickx);
temp = tickx;
tickx = cell(1,size(temp,1));
for j=1:size(temp,1);
tickx{j} = strtrim( temp(j,:) );
end;
end;
append = '^{\circ}';
for j=1:length(tickx);
tickx{j} = [tickx{j} append];
end;
elseif isstr(tickx);
append = tickx;
tickx = get(h,'XTickLabel');
if ischar(tickx);
temp = tickx;
tickx = cell(1,size(temp,1));
for j=1:size(temp,1);
tickx{j} = strtrim( temp(j,:) );
end;
end;
if strcmp(append(1),'$');
for j=1:length(tickx);
tickx{j} = ['$' tickx{j} append(2:end)];
end;
else;
for j=1:length(tickx);
tickx{j} = [tickx{j} append];
end;
end;
elseif ~iscell(tickx );
warning(['Input TICKX variable is not a compatible string ' ...
'or cell array! Returning...']);
return;
end;
%find out if we have to use the LaTex interpreter
temp = tickx{1};
if strcmp(temp(1),'$');
latex_on = 1;
else;
latex_on = 0;
end;
%erase the current tick label
set(h,'XTickLabel',{});
%get the x tick positions if the user did not input them
if ~exist('tickposx','var');
tickposx = get(h,'XTick');
elseif length(tickx) == 0;
tickposx = get(h,'XTick');
end;
%get the y tick positions if the user did not input them
if ~exist('tickposy','var');
tickposy = get(h,'YTick');
elseif length(tickposy) == 0;
tickposy = get(h,'YTick');
end;
%set the new tick positions
set(h,'YTick',tickposy);
set(h,'XTick',tickposx);
%check the lengths of the xtick positions and xtick labels
l1 = length(tickx);
l2 = length(tickposx);
if l1==0;
set(h,'XTickLabel',tickx);
end;
if l1~=l2;
disp(['Length of XTick = ' num2str(length(tickposx))]);
disp(['Length of XTickLabel = ' num2str(length(tickx))]);
if l2 < l1;
warning(['Reducing Length of XTickLabel!']);
else;
warning(['Reducing Length of XTick!']);
end;
l3 = min([l1,l2]);
tickx = tickx{1:l3};
tickposx = tickposx(1:l3);
end;
%set rotation to 0 if not input
if ~exist('rotx','var');
rotx = 0;
elseif length(rotx) == 0;
rotx = 0;
end;
%Convert the cell labels to a character string
%tickx = char(tickx);
tickx = cellstr(tickx);
%Make the XTICKS!
lim = get(h,'YLim');
if min(tickposy) < lim(1);
lim(1) = min(tickposy);
end;
if max(tickposy) > lim(2);
lim(2) = max(tickposy);
end;
if rotx == 0;
if latex_on;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposx),1),...
tickx,'HorizontalAlignment','center',...
'VerticalAlignment','top','rotation',rotx,'interpreter','LaTex');
else;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposx),1),...
tickx,'HorizontalAlignment','center',...
'VerticalAlignment','top','rotation',rotx);
end;
elseif rotx < 0;
if latex_on;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposx),1),...
tickx,'HorizontalAlignment','left','interpreter','LaTex',...
'VerticalAlignment','middlefi','rotation',rotx);
else;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposx),1),...
tickx,'HorizontalAlignment','left',...
'VerticalAlignment','middle','rotation',rotx);
end;
else;
if latex_on;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposx),1),...
tickx,'HorizontalAlignment','right','interpreter','LaTex',...
'VerticalAlignment','middle','rotation',rotx);
else;
hx = text(tickposx,...
repmat(lim(1)-offset*(lim(2)-lim(2)),length(tickposx),1),...
tickx,'HorizontalAlignment','right',...
'VerticalAlignment','middle','rotation',rotx);
end;
end;
%Get and set the text size and weight
set(hx,'FontSize',get(h,'FontSize'));
set(hx,'FontWeight',get(h,'FontWeight'));
%Set the additional parameters if they were input
if length(varargin) > 2;
command_string = ['set(hx'];
for j=1:2:length(varargin);
command_string = [command_string ',' ...
'''' varargin{j} ''',varargin{' num2str(j+1) '}'];
end;
command_string = [command_string ');'];
eval(command_string);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%END: FIRST THE X-AXIS TICK LABELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%BEGIN: NOW THE Y-AXIS TICK LABELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%only move forward if we are doing anything to the yticks
if ~exist('ticky');
hy = -1;
elseif length(ticky)==0;
hy = -1;
else;
%fix the YTickLabels if they have been erased in the past
if length(get(h,'YTickLabel'))==0;
set(h,'YTickLabel',get(h,'YTick'));
end;
%set the ytick positions if entered
if exist('tickposy','var');
if length(tickposy) > 0;
set(h,'YTick',tickposy);
set(h,'YTickLabel',tickposy);
end;
end;
%make sure the xtick positions are in the xlimit range
if exist('tickposy','var');
if length(tickposy) > 0;
lim = get(h,'YLim');
if lim(1) > min(tickposy);
lim(1) = min(tickposy);
end;
if lim(2) < max(tickposy);
lim(2) = max(tickposy);
end;
set(h,'YLim',lim);
end;
end;
%get the tick labels and positions if the user did not input them
if ~exist('ticky','var');
ticky = get(h,'YTickLabel');
if ischar(ticky);
temp = ticky;
ticky = cell(1,size(temp,1));
for j=1:size(temp,1);
ticky{j} = strtrim( temp(j,:) );
end;
end;
append = '^{\circ}';
for j=1:length(ticky);
ticky{j} = [ticky{j} append];
end;
elseif length(ticky) == 0;
ticky = get(h,'YTickLabel');
if ischar(ticky);
temp = ticky;
ticky = cell(1,size(temp,1));
for j=1:size(temp,1);
ticky{j} = strtrim( temp(j,:) );
end;
end;
append = '^{\circ}';
for j=1:length(ticky);
ticky{j} = [ticky{j} append];
end;
elseif isstr(ticky);
append = ticky;
ticky = get(h,'YTickLabel');
if ischar(ticky);
temp = ticky;
ticky = cell(1,size(temp,1));
for j=1:size(temp,1);
ticky{j} = strtrim( temp(j,:) );
end;
end;
if strcmp(append(1),'$');
for j=1:length(ticky);
ticky{j} = ['$' ticky{j} append(2:end)];
end;
else;
for j=1:length(ticky);
ticky{j} = [ticky{j} append];
end;
end;
elseif ~iscell(ticky );
warning(['Input TICKY variable is not a compatible string ' ...
'or cell array! Returning...']);
return;
end;
%find out if we have to use the LaTex interpreter
temp = ticky{1};
if strcmp(temp(1),'$');
latex_on = 1;
else;
latex_on = 0;
end;
%erase the current tick label
set(h,'YTickLabel',{});
%get the x tick positions if the user did not input them
if ~exist('tickposy','var');
tickposy = get(h,'YTick');
elseif length(ticky) == 0;
tickposy = get(h,'YTick');
end;
%get the x tick positions if the user did not input them
if ~exist('tickposx','var');
tickposx = get(h,'YTick');
elseif length(tickposx) == 0;
tickposx = get(h,'XTick');
end;
%set the new tick positions
set(h,'YTick',tickposy);
% set(h,'XTick',tickposx);
%check the lengths of the xtick positions and xtick labels
l1 = length(ticky);
l2 = length(tickposy);
if l1==0;
set(h,'YTickLabel',ticky);
end;
if l1~=l2;
disp(['Length of YTick = ' num2str(length(tickposy))]);
disp(['Length of YTickLabel = ' num2str(length(ticky))]);
if l2 < l1;
warning(['Reducing Length of YTickLabel!']);
else;
warning(['Reducing Length of YTick!']);
end;
l3 = min([l1,l2]);
ticky = ticky{1:l3};
tickposy = tickposy(1:l3);
end;
%set rotation to 0 if not input
if ~exist('roty','var');
roty = 0;
elseif length(roty) == 0;
roty = 0;
end;
%Convert the cell labels to a character string
%ticky = char(ticky);
ticky = cellstr(ticky);
%Make the YTICKS!
lim = get(h,'XLim');
if min(tickposx) < lim(1);
lim(1) = min(tickposx);
end;
if max(tickposx) > lim(2);
lim(2) = max(tickposx);
end;
if roty == 0;
if latex_on;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty,'interpreter','LaTex');
else;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty);
end;
elseif roty < 180;
if latex_on;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty,'interpreter','LaTex');
else;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty);
end;
else;
if latex_on;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty,'interpreter','LaTex');
else;
hy = text(...
repmat(lim(1)-offset*(lim(2)-lim(1)),length(tickposy),1),...
tickposy,...
ticky,'VerticalAlignment','middle',...
'HorizontalAlignment','right','rotation',roty);
end;
end;
%Get and set the text size and weight
set(hy,'FontSize',get(h,'FontSize'));
set(hy,'FontWeight',get(h,'FontWeight'));
%Set the additional parameters if they were input
if length(varargin) > 2;
command_string = ['set(hy'];
for j=1:2:length(varargin);
command_string = [command_string ',' ...
'''' varargin{j} ''',varargin{' num2str(j+1) '}'];
end;
command_string = [command_string ');'];
eval(command_string);
end;
end;