Daniel@0: function [nAxis] = addTopXAxis (varargin) Daniel@0: Daniel@0: % [nAxis] = addTopXAxis (axisH, properties ...) Daniel@0: % Add an X-axis on top (additional to the one on bottom) of the figure, Daniel@0: % with its own ticks labels and axis label (allow double scale). Daniel@0: % Ticks, minor ticks positions and x limits are set to be identical for top and bottom Daniel@0: % axis. Moreover, they are linked, i.e. a modification of one of them, even after execution Daniel@0: % of this function, will result in modification of the other one. Daniel@0: % Daniel@0: % Parameters: Daniel@0: % axisH = handle for axes (dafault = current axes, given by |gca|) Daniel@0: % properties: syntax is two-folded arguments for each property, Daniel@0: % on the form: 'name of property' (case insensitive), value of property Daniel@0: % and properties can be Daniel@0: % xLabStr : the label for the top X-axis (string) Daniel@0: % default = '' Daniel@0: % expression or exp : expression for transforming bottom X-tick labels to Daniel@0: % top X-tick labels. In the expression, |argu| will refer Daniel@0: % to the value of the bottom X-tick labels. Daniel@0: % eg. going from |log10(x)| to linear |x| values will be done with Daniel@0: % the string '10.^argu' for expression. Daniel@0: % default = '' Daniel@0: % Note : if expression needs to use some variables, that are not accessible to this function, they need to be evaluated Daniel@0: % before being passed to this function (see exemples below). Daniel@0: % xTickLabelFormat : display format, used for converting numerical values for tick labels in strings. (string) Daniel@0: % The computation of top axis tick labels using |expression| can lead to numbers taking a lot of space Daniel@0: % because of the precision of the display. Reducing the precision will reduce the size of the labels. Daniel@0: % default = '%4.1f' Daniel@0: % Daniel@0: % Exemples: Daniel@0: % Daniel@0: % axisH = axes % create new axes, and save the handle in axisH Daniel@0: % addTopXAxis(axisH, 'expression', '-argu') % change the label of the ticks by their opposite Daniel@0: % Daniel@0: % Daniel@0: % addTopXAxis('xticklabel', '(k0.*10.^argu)', 'xlabel', '$\lambda_{up}$ (cm)') Daniel@0: % will use the current axis (handle is not passed to the function), and will compute the new X-tick values according to Daniel@0: % x' = k0.*10.^argu; Daniel@0: % where |k0| is a variable whose value has to be set in the 'base' workspace. Daniel@0: Daniel@0: % Daniel@0: % V2 - 11/27/2005 Daniel@0: % Modifs for V2: Daniel@0: % * - now evaluates expression in 'base' workspace. Daniel@0: % Therefore, variables (like 'k0' in the second example) do not need to be evaluated anymore before being passed, Daniel@0: % as long as they already exist in 'base' workspace. It should allow more complex expressions to be passed than previously. Daniel@0: % Example 2 then becomes Daniel@0: % addTopXAxis('expression', '(k0.*10.^argu)', 'xLabStr', '$\lambda_{up}$ (cm)') Daniel@0: % instead of Daniel@0: % addTopXAxis('expression', ['(', num2str(k0),'.*10.^argu)'], 'xLabStr', '$\lambda_{up}$ (cm)' Daniel@0: % Drawback: the function create/assign a variable called 'axisH' in base workspace. Potential conflicts here ... Daniel@0: % * - properties are not case sensitive anymore Daniel@0: % * - 'exp' can be used instead of 'expression' for property name (following John D'Errico comment, if I understood it well ....) Daniel@0: % Daniel@0: % Daniel@0: % Author : Emmanuel P. Dinnat Daniel@0: % Date : 09/2005 Daniel@0: % Contact: emmanueldinnat@yahoo.fr Daniel@0: Daniel@0: global hlink % make the properties link global Daniel@0: Daniel@0: %% Default values for properties Daniel@0: axisH = gca; Daniel@0: xTickLabelFormat = '%4.1f'; Daniel@0: xLabStr = ''; Daniel@0: expression = ''; Daniel@0: Daniel@0: %% Process input parameters (if they exist) Daniel@0: % if input parameters Daniel@0: if length(varargin) > 0 Daniel@0: if isstr(varargin{1}) % if no axes handle is passed ... Daniel@0: axisH = gca; Daniel@0: if (length(varargin) > 1) Daniel@0: properties = varargin(1:end); Daniel@0: end Daniel@0: else % else deal with passed axes handle Daniel@0: if ishandle(varargin{1}) Daniel@0: axisH = varargin{1}; Daniel@0: else Daniel@0: error('addTopXAxis : handle for axes invalid.') Daniel@0: end Daniel@0: properties = varargin(2:end); Daniel@0: end Daniel@0: if ~mod(length(properties),2) Daniel@0: for iArg = 1:length(properties)/2 Daniel@0: % switch properties{2*iArg-1} Daniel@0: switch lower(properties{2*iArg-1}) % modif V2 - suppress case sensitivity Daniel@0: case {'xticklabel','expression' , 'exp'} Daniel@0: expression = properties{2*iArg}; Daniel@0: % case 'xLabStr' Daniel@0: case {'xlabel','xlabstr'} % V2 Daniel@0: xLabStr = properties{2*iArg}; Daniel@0: % case 'xTickLabelFormat' Daniel@0: case 'xticklabelformat' % V2 Daniel@0: xTickLabelFormat = properties{2*iArg}; Daniel@0: otherwise Daniel@0: error(['addTopXAxis : property ''', properties{2*iArg-1},''' does not exist.']) Daniel@0: end Daniel@0: end Daniel@0: else Daniel@0: error('addTopXAxis : arguments number for proporties should be even.') Daniel@0: end Daniel@0: end % if input parameters Daniel@0: %% replace |argu| by x-tick labels in the computation expression for new x-tick labels Daniel@0: % newXtickLabel_command = regexprep(expression, 'argu', 'get(axisH, ''xTick'')'''); Daniel@0: Daniel@0: %% Get paramters of figures to be modified (other parameters to be copied on the new axis are not extracted) Daniel@0: set(axisH, 'units', 'normalized'); Daniel@0: cAxis_pos = get(axisH, 'position'); Daniel@0: %% shift downward original axis a little bit Daniel@0: cAxis_pos(2) = cAxis_pos(2)*0.8; Daniel@0: set(axisH, 'position', cAxis_pos); Daniel@0: %% Make new axis Daniel@0: nAxis = subplot('position', [cAxis_pos(1), (cAxis_pos(2)+cAxis_pos(4))*1.007, cAxis_pos(3), 0.0001]); Daniel@0: %% put new Xaxis on top Daniel@0: set(nAxis, 'xaxisLocation', 'top'); Daniel@0: %% Improve readability Daniel@0: %% delete Y label on new axis Daniel@0: set(nAxis, 'yTickLabel', []); Daniel@0: % remove box for original axis Daniel@0: % set(axisH, 'box', 'off'); Daniel@0: % remove grids Daniel@0: set(nAxis, 'yGrid', 'off'); Daniel@0: set(nAxis, 'xGrid', 'off'); Daniel@0: %% Set new Xaxis limits, ticks and subticks the same as original ones (by link) ... Daniel@0: set(nAxis, 'xlim', get(axisH, 'xlim')); Daniel@0: set(nAxis, 'XTick', get(axisH, 'XTick')); Daniel@0: set(nAxis, 'XMinorTick', get(axisH, 'XMinorTick')); Daniel@0: hlink = linkprop([nAxis, axisH], {'xLim','XTick','XMinorTick'}); Daniel@0: %% ... but replace ticks labels by new ones !!! Daniel@0: assignin('base', 'axisH', axisH) Daniel@0: % set(nAxis, 'xtickLabel', num2str(evalin('base', newXtickLabel_command), xTickLabelFormat)); Daniel@0: set(nAxis, 'xtickLabel', expression); Daniel@0: %% but label for new axis Daniel@0: if exist('xLabStr') Daniel@0: xlabel(xLabStr) Daniel@0: end Daniel@0: %% return current axis to original one (for further modification affecting original axes) Daniel@0: axes(axisH); Daniel@0: % hlink = linkprop([nAxis, gca], {'xLim','XTick','XMinorTick'})