-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathy_to_norm.m
136 lines (121 loc) · 5.36 KB
/
y_to_norm.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
%author: Girish Ratanpal, ECE, UVa.
%transform axes units to normalized units for 2-D figures only.
%works for linear,log scales and also reverse axes.
%DATE: JAN. 6, 2006. previous version: aug 12, 2005.
%
%
%FUNCTION DESCRIPTION:
% function [y_norm] = y_to_norm_v2(y_begin,y_end)
% function returns a 1x2 array, y_norm, with normalized unitsy_begin and
% y_end for the annotation object.
%
% function arguments:
%1. y_begin: enter the point where the annotation object begins on the axis
% using axis units
%2. y_end: end of annotation object, again in axis units.
%
%EXAMPLE: first plot the graph on the figure.
%then use the following command for placing an arrow:
%h_object =
%annotation('arrow',x_to_norm_v2(x_begin,x_end),y_to_norm_v2(y_begin,y_end));
%******************************************
%CODE FOR x_norm_v2() IS COMMENTED AT THE END.
%******************************************
function [y_norm] = y_to_norm_v2(y_begin,y_end)
if nargin ~= 2
error('Wrong number of input arguments! y_to_norm_v2(y_begin,y_end)')
end
h_axes = get(gcf,'CurrentAxes'); %get axes handle.
axesoffsets = get(h_axes,'Position');%get axes position on the figure.
y_axislimits = get(gca, 'ylim'); %get axes extremeties.
y_dir = get(gca,'ydir');
y_scale = get(gca,'YScale');
%get axes length
y_axislength_lin = abs(y_axislimits(2) - y_axislimits(1));
if strcmp(y_dir,'normal') %axis not reversed
if strcmp(y_scale,'log')
%get axes length in log scale.
y_axislength_log = abs(log10(y_axislimits(2)) - log10(y_axislimits(1)));
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_begin)-log10(y_axislimits(1)))/(y_axislength_log);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_end)-log10(y_axislimits(1)))/(y_axislength_log);
y_norm = [y_begin_norm y_end_norm];
elseif strcmp(y_scale,'linear')%linear scale.
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs((y_begin-y_axislimits(1))/y_axislength_lin);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs((y_end-y_axislimits(1))/y_axislength_lin);
y_norm = [y_begin_norm y_end_norm];
else
error('use only lin or log in quotes for scale')
end
elseif strcmp(ydir,'reverse') %axis is reversed
if strcmp(y_scale,'log')
%get axes length in log scale.
y_axislength_log = abs(log10(y_axislimits(2)) - log10(y_axislimits(1)));
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_axislimits(2))-log10(y_begin))/(y_axislength_log);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs(log10(y_axislimits(2))-log10(y_end))/(y_axislength_log);
y_norm = [y_begin_norm y_end_norm];
elseif strcmp(y_scale,'linear')
%normalized distance from the lower left corner of figure.
y_begin_norm = axesoffsets(2)+axesoffsets(4)*abs((y_axislimits(2)-y_begin)/y_axislength_lin);
y_end_norm = axesoffsets(2)+axesoffsets(4)*abs((y_axislimits(2)-y_end)/y_axislength_lin);
y_norm = [y_begin_norm y_end_norm];
else
error('use only lin or log in quotes for scale')
end
else
error('use only r or nr in quotes for reverse')
end
%********************************************************
% function [x_norm] = x_to_norm_v2(x_begin,x_end)
%
% if nargin ~= 2
% error('Wrong number of input arguments.')
% end
%
% h_axes = get(gcf,'CurrentAxes'); %get axes handle
%
% axesoffsets = get(h_axes,'Position');
% x_axislimits = get(gca, 'xlim'); %get axes extremeties.
% x_dir = get(gca,'xdir');
% x_scale = get(gca,'xscale');
%
% %get axes length
% x_axislength_lin = abs(x_axislimits(2) - x_axislimits(1));
%
%
% if strcmp(x_dir,'normal')
% if strcmp(x_scale,'log')
% x_axislength_log = abs(log10(x_axislimits(2)) - log10(x_axislimits(1)));
% x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_begin)-log10(x_axislimits(1)))/(x_axislength_log);
% x_end_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_end)-log10(x_axislimits(1)))/(x_axislength_log);
%
% x_norm = [x_begin_norm x_end_norm];
% elseif strcmp(x_scale,'linear')
% x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs((x_begin-x_axislimits(1))/x_axislength_lin);
% x_end_norm = axesoffsets(1)+axesoffsets(3)*abs((x_end-x_axislimits(1))/x_axislength_lin);
%
% x_norm = [x_begin_norm x_end_norm];
% else
% error('use only lin or log in quotes for scale')
% end
%
% elseif strcmp(x_dir,'reverse')
% if strcmp(x_scale,'log')
% x_axislength_log = abs(log10(x_axislimits(2)) - log10(x_axislimits(1)));
% x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_axislimits(2))-log10(x_begin))/(x_axislength_log);
% x_end_norm = axesoffsets(1)+axesoffsets(3)*abs(log10(x_axislimits(2))-log10(x_end))/(x_axislength_log);
%
% x_norm = [x_begin_norm x_end_norm];
% elseif strcmp(x_scale,'linear')
% x_begin_norm = axesoffsets(1)+axesoffsets(3)*abs((x_axislimits(2)-x_begin)/x_axislength_lin);
% x_end_norm = axesoffsets(1)+axesoffsets(3)*abs((x_axislimits(2)-x_end)/x_axislength_lin);
%
% x_norm = [x_begin_norm x_end_norm];
% else
% error('use only lin or log in quotes for scale')
% end
% else
% error('use only r or nr in quotes for reverse')
% end