-
Notifications
You must be signed in to change notification settings - Fork 0
/
vekLeg.m
86 lines (75 loc) · 3.2 KB
/
vekLeg.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
function vekLeg(Location, scale, arrow_value, line,mark)
%VEKLEG - Produces a "legend" like scale arrow as produced by vekplot2.m
%
% Syntax: vekLeg(Location, scale)
%
% Inputs:
% Location - Location of the vector legend in format like legend
% 'Location'
% scale - Length of arrow in the figure when u^2 + v^2 = 1. Should be
% same as vekplot2 scale
% arrow_value - Arrow representing this velocity (in m/s)
%
% Outputs:
% N/A
%
% Example:
% vekLeg('NorthEast', 1, 1)
%
% Other m-files required: vekplot2.m
% Subfunctions: n/a
% MAT-files required: n/a
%
% See also: vekplot2.m
% Author: Sam Hartharn-Evans
% Newcastle University - s.hartharn-evans2@ncl.ac.uk
% April 2020; Last Revision 07/04/2020
% Version 1.0
% -------------------------------------------------------------------------
%% Check Variables Set
% -------------------------------------------------------------------------
if nargin < 4 || isempty(line)
line = 'b';
end
if nargin <5 || isempty(mark)
mark = '-';
end
% -------------------------------------------------------------------------
%% Begin Code
% -------------------------------------------------------------------------
% Open a legend, record it's position and close it
f = gca; % Call current axis, open one if not present
l = legend('Location', Location);
Position_leg = l.Position;
old_axesPos = f.Position; % Also record the revised position of the axis
% so that changes in this due to placement of
% the legend can be retained
delete(l); % Delete the legend again
if isa((f.Parent), 'matlab.graphics.layout.TiledChartLayout')
f.Position = old_axesPos; % Reposition main axes as if a legend had been placed there
end
% Calculate width of figure in x units
f_xunit_width = diff(f.XLim); % Width of plot in data units
f_axes_width = f.InnerPosition(3); % Width of plot in figure units
%xUnitChangeInFigure = f_xunit_width/f_axes_width; % Change in data units for 1 change in figure units
DataToFigure = f_axes_width/f_xunit_width;
%FigureToData = f_xunit_width/f_axes_width; %
% Calculate required width of box
t = text(6 , .15, strcat([num2str(arrow_value), ' m/s']), 'Horizontalalignment', 'center');
box_width_dataUnits = max(scale*arrow_value, t.Extent(3));
box_width = box_width_dataUnits*DataToFigure*1.3;
%% Produce the vector legend
g = axes('InnerPosition', [Position_leg(1), Position_leg(2), box_width, Position_leg(4)*.7], 'Box', 'on'); % Plot in the space previously identified by the legend creation
relative_height = Position_leg(4)/f.Position(4)*diff(f.YLim);
vekplot2(0, relative_height/3, arrow_value, 0, scale, line, mark); % Produce an arrow 1 unit in size, scaled as per "scale"
AxesXLim = [0 box_width_dataUnits]-(box_width_dataUnits-scale*arrow_value)/2; %scale*arrow_value]
xlim(g, AxesXLim);
ylim(g, [0 relative_height])
delete(t);
text(g, mean(AxesXLim) , relative_height/2, strcat([num2str(arrow_value), ' m/s']), 'Horizontalalignment', 'center');
% Remove axis like formatting
xticks([]); yticks([]); box on
g.Position = [Position_leg(1), Position_leg(2), Position_leg(3), Position_leg(4)*.7];
% Restore current axes to the main axes just in case this is needed
set(gcf, 'CurrentAxes', f);
end