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