comparison core/tools/addTopXAxis.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [nAxis] = addTopXAxis (varargin)
2
3 % [nAxis] = addTopXAxis (axisH, properties ...)
4 % Add an X-axis on top (additional to the one on bottom) of the figure,
5 % with its own ticks labels and axis label (allow double scale).
6 % Ticks, minor ticks positions and x limits are set to be identical for top and bottom
7 % axis. Moreover, they are linked, i.e. a modification of one of them, even after execution
8 % of this function, will result in modification of the other one.
9 %
10 % Parameters:
11 % axisH = handle for axes (dafault = current axes, given by |gca|)
12 % properties: syntax is two-folded arguments for each property,
13 % on the form: 'name of property' (case insensitive), value of property
14 % and properties can be
15 % xLabStr : the label for the top X-axis (string)
16 % default = ''
17 % expression or exp : expression for transforming bottom X-tick labels to
18 % top X-tick labels. In the expression, |argu| will refer
19 % to the value of the bottom X-tick labels.
20 % eg. going from |log10(x)| to linear |x| values will be done with
21 % the string '10.^argu' for expression.
22 % default = ''
23 % Note : if expression needs to use some variables, that are not accessible to this function, they need to be evaluated
24 % before being passed to this function (see exemples below).
25 % xTickLabelFormat : display format, used for converting numerical values for tick labels in strings. (string)
26 % The computation of top axis tick labels using |expression| can lead to numbers taking a lot of space
27 % because of the precision of the display. Reducing the precision will reduce the size of the labels.
28 % default = '%4.1f'
29 %
30 % Exemples:
31 %
32 % axisH = axes % create new axes, and save the handle in axisH
33 % addTopXAxis(axisH, 'expression', '-argu') % change the label of the ticks by their opposite
34 %
35 %
36 % addTopXAxis('xticklabel', '(k0.*10.^argu)', 'xlabel', '$\lambda_{up}$ (cm)')
37 % will use the current axis (handle is not passed to the function), and will compute the new X-tick values according to
38 % x' = k0.*10.^argu;
39 % where |k0| is a variable whose value has to be set in the 'base' workspace.
40
41 %
42 % V2 - 11/27/2005
43 % Modifs for V2:
44 % * - now evaluates expression in 'base' workspace.
45 % Therefore, variables (like 'k0' in the second example) do not need to be evaluated anymore before being passed,
46 % as long as they already exist in 'base' workspace. It should allow more complex expressions to be passed than previously.
47 % Example 2 then becomes
48 % addTopXAxis('expression', '(k0.*10.^argu)', 'xLabStr', '$\lambda_{up}$ (cm)')
49 % instead of
50 % addTopXAxis('expression', ['(', num2str(k0),'.*10.^argu)'], 'xLabStr', '$\lambda_{up}$ (cm)'
51 % Drawback: the function create/assign a variable called 'axisH' in base workspace. Potential conflicts here ...
52 % * - properties are not case sensitive anymore
53 % * - 'exp' can be used instead of 'expression' for property name (following John D'Errico comment, if I understood it well ....)
54 %
55 %
56 % Author : Emmanuel P. Dinnat
57 % Date : 09/2005
58 % Contact: emmanueldinnat@yahoo.fr
59
60 global hlink % make the properties link global
61
62 %% Default values for properties
63 axisH = gca;
64 xTickLabelFormat = '%4.1f';
65 xLabStr = '';
66 expression = '';
67
68 %% Process input parameters (if they exist)
69 % if input parameters
70 if length(varargin) > 0
71 if isstr(varargin{1}) % if no axes handle is passed ...
72 axisH = gca;
73 if (length(varargin) > 1)
74 properties = varargin(1:end);
75 end
76 else % else deal with passed axes handle
77 if ishandle(varargin{1})
78 axisH = varargin{1};
79 else
80 error('addTopXAxis : handle for axes invalid.')
81 end
82 properties = varargin(2:end);
83 end
84 if ~mod(length(properties),2)
85 for iArg = 1:length(properties)/2
86 % switch properties{2*iArg-1}
87 switch lower(properties{2*iArg-1}) % modif V2 - suppress case sensitivity
88 case {'xticklabel','expression' , 'exp'}
89 expression = properties{2*iArg};
90 % case 'xLabStr'
91 case {'xlabel','xlabstr'} % V2
92 xLabStr = properties{2*iArg};
93 % case 'xTickLabelFormat'
94 case 'xticklabelformat' % V2
95 xTickLabelFormat = properties{2*iArg};
96 otherwise
97 error(['addTopXAxis : property ''', properties{2*iArg-1},''' does not exist.'])
98 end
99 end
100 else
101 error('addTopXAxis : arguments number for proporties should be even.')
102 end
103 end % if input parameters
104 %% replace |argu| by x-tick labels in the computation expression for new x-tick labels
105 % newXtickLabel_command = regexprep(expression, 'argu', 'get(axisH, ''xTick'')''');
106
107 %% Get paramters of figures to be modified (other parameters to be copied on the new axis are not extracted)
108 set(axisH, 'units', 'normalized');
109 cAxis_pos = get(axisH, 'position');
110 %% shift downward original axis a little bit
111 cAxis_pos(2) = cAxis_pos(2)*0.8;
112 set(axisH, 'position', cAxis_pos);
113 %% Make new axis
114 nAxis = subplot('position', [cAxis_pos(1), (cAxis_pos(2)+cAxis_pos(4))*1.007, cAxis_pos(3), 0.0001]);
115 %% put new Xaxis on top
116 set(nAxis, 'xaxisLocation', 'top');
117 %% Improve readability
118 %% delete Y label on new axis
119 set(nAxis, 'yTickLabel', []);
120 % remove box for original axis
121 % set(axisH, 'box', 'off');
122 % remove grids
123 set(nAxis, 'yGrid', 'off');
124 set(nAxis, 'xGrid', 'off');
125 %% Set new Xaxis limits, ticks and subticks the same as original ones (by link) ...
126 set(nAxis, 'xlim', get(axisH, 'xlim'));
127 set(nAxis, 'XTick', get(axisH, 'XTick'));
128 set(nAxis, 'XMinorTick', get(axisH, 'XMinorTick'));
129 hlink = linkprop([nAxis, axisH], {'xLim','XTick','XMinorTick'});
130 %% ... but replace ticks labels by new ones !!!
131 assignin('base', 'axisH', axisH)
132 % set(nAxis, 'xtickLabel', num2str(evalin('base', newXtickLabel_command), xTickLabelFormat));
133 set(nAxis, 'xtickLabel', expression);
134 %% but label for new axis
135 if exist('xLabStr')
136 xlabel(xLabStr)
137 end
138 %% return current axis to original one (for further modification affecting original axes)
139 axes(axisH);
140 % hlink = linkprop([nAxis, gca], {'xLim','XTick','XMinorTick'})