Mercurial > hg > ltpda
diff m-toolbox/classes/@unit/tolabel.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/@unit/tolabel.m Wed Nov 23 19:22:13 2011 +0100 @@ -0,0 +1,65 @@ +% TOLABEL converts a unit object to LaTeX string suitable for use as axis labels. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% DESCRIPTION: TOLABEL converts a unit object to LaTeX string suitable for +% use as axis labels. +% +% CALL: lbl = tolabel(u) +% +% VERSION: $Id: tolabel.m,v 1.9 2011/02/18 16:48:55 ingo Exp $ +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +function varargout = tolabel(varargin) + + objs = [varargin{:}]; + + pstrs = {}; + + for ii = 1:numel(objs) + u = objs(ii); + + [num, den] = factor(u); + + numstr = formatUnit(num); + denstr = formatUnit(den); + + if isempty(denstr) + str =['$$\left[' numstr ' \right]$$']; + else + if isempty(numstr) + str =['$$\left[1/' denstr ' \right]$$']; + else + str =['$$\left[\frac{' numstr '}{' denstr '} \right]$$']; + end + end + + pstrs = [pstrs {str}]; + + end + + varargout{1} = pstrs; +end + + +function s = formatUnit(u) + + s = ''; + for kk=1:numel(u.strs) + prefix = unit.val2prefix(u.vals(kk)); + if u.exps(kk) == 0.5 + s = [s '\,{\sqrt{\textrm{' prefix u.strs{kk} '}}}']; + elseif u.exps(kk) == 1 + s = [s '\,{\textrm{' prefix u.strs{kk} '}}']; + elseif u.exps(kk) > 0 + [n,d] = rat(u.exps(kk)); + if d == 1 + s = [s '\,{\textrm{' prefix u.strs{kk} '}}^{' num2str(n) '}']; + else + s = [s '\,{\textrm{' prefix u.strs{kk} '}}^{' num2str(n) '/' num2str(d) '}']; + end + end + end + +end +