V_SPRINTSI Print X with SI multiplier S=(X,D,W,U) Usage: (1) v_sprintsi(2345,-2) -> '2.3 k' (2) v_sprintsi(2345,-2,8,' ') -> ' 2.3 k' (3) v_sprintsi(2345,-2,[],'Hz') -> '2.3kHz' Inputs: X the value to print D number of decimal places (+ve) or significant digits (-ve) [-3] W add leading spaces so that |W| is minimum total width including multiplier [0] If W<=0 then trailing 0's will be eliminated. U string giving the unit (if present, an initial space will be placed before the multiplier) [' '] Outputs: S output string
0001 function s=v_sprintsi(x,d,w,u) 0002 %V_SPRINTSI Print X with SI multiplier S=(X,D,W,U) 0003 % 0004 % Usage: (1) v_sprintsi(2345,-2) -> '2.3 k' 0005 % (2) v_sprintsi(2345,-2,8,' ') -> ' 2.3 k' 0006 % (3) v_sprintsi(2345,-2,[],'Hz') -> '2.3kHz' 0007 % 0008 % Inputs: X the value to print 0009 % D number of decimal places (+ve) or significant digits (-ve) [-3] 0010 % W add leading spaces so that |W| is minimum total width including multiplier [0] 0011 % If W<=0 then trailing 0's will be eliminated. 0012 % U string giving the unit (if present, an initial space will be placed before the multiplier) [' '] 0013 % 0014 % Outputs: S output string 0015 0016 % Copyright (C) Mike Brookes 1998-2022 0017 % Version: $Id: v_sprintsi.m 10865 2018-09-21 17:22:45Z dmb $ 0018 % 0019 % VOICEBOX is a MATLAB toolbox for speech processing. 0020 % Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html 0021 % 0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0023 % This program is free software; you can redistribute it and/or modify 0024 % it under the terms of the GNU General Public License as published by 0025 % the Free Software Foundation; either version 2 of the License, or 0026 % (at your option) any later version. 0027 % 0028 % This program is distributed in the hope that it will be useful, 0029 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0030 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0031 % GNU General Public License for more details. 0032 % 0033 % You can obtain a copy of the GNU General Public License from 0034 % http://www.gnu.org/copyleft/gpl.html or by writing to 0035 % Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. 0036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0037 persistent f f0 emin emax 0038 if isempty(f) 0039 f='yzafpnum kMGTPEZY'; % list of SI multipliers 0040 f0=find(f==' '); % position of space in list of SI multipliers 0041 emin=3-3*f0; % lowest power of 10 in f 0042 emax=3*(length(f)-f0); % highest power of 10 in f 0043 end 0044 if nargin<4 % if no unit string specified 0045 u=' '; % default unit string is a space 0046 end 0047 if nargin<3 || isempty(w) % if no width is specified 0048 w=0; % default is 0, i.e. eliminate trainling zeros 0049 end 0050 if nargin<2 || isempty(d) 0051 d=-3; % default precision is 3 significant figures 0052 end 0053 % need to check what happens if x=0 to avoid "y" prefix 0054 if x==0 0055 e=0; 0056 else 0057 e=floor(log10(abs(x))); % highest power of 10 <= abs(x) 0058 end 0059 k=floor(max(emin,min(emax,e))/3); % SI multilier to use is f(k+f0) 0060 dp=max([0 d 3*k-d-e-1]); % number of decimal places to use 0061 if w<=0 & dp 0062 w=abs(w); 0063 dp=max(find([1 mod(mod(round(x*10^(dp-3*k)),10^dp),10.^(dp:-1:1))]))-1; 0064 end 0065 if length(u)>0 && u(1)==' ' % unit string starts with a space 0066 if(k) % mutliplier needed 0067 s=sprintf(sprintf('%%%d.%df %c%s',max(w-2,0),dp,f(k+f0),u(2:end)),x*1e-3^k); 0068 else % no mutliplier needed 0069 s=sprintf(sprintf('%%%d.%df %s',max(w-1,0),dp,u(2:end)),x*1e-3^k); 0070 end 0071 else % no space before unit string 0072 if(k) % mutliplier needed 0073 s=sprintf(sprintf('%%%d.%df%c%s',max(w-2,0),dp,f(k+f0),u),x*1e-3^k); 0074 else % no mutliplier needed 0075 s=sprintf(sprintf('%%%d.%df%s',max(w-1,0),dp,u),x*1e-3^k); 0076 end 0077 end