v_sprintsi

PURPOSE ^

V_SPRINTSI Print X with SI multiplier S=(X,D,W,U)

SYNOPSIS ^

function s=v_sprintsi(x,d,w,u)

DESCRIPTION ^

V_SPRINTSI Print X with SI multiplier S=(X,D,W,U)
 D is number of decimal places (+ve) or significant digits (-ve) [default=-3]
 |W| is total width including multiplier
 if W<=0 then trailing 0's will be eliminated
 U give a unit string (any initial space will be placed before the multiplier) [default ' ']

 Example: v_sprintsi(2345,-2) gives '2.3 k'
V_SPGRAMBW Draw spectrogram [T,F,B]=(s,fs,mode,bw,fmax,db,tinc,ann)

  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) [default=-3]
           W        add leading spaces so that |W| is total width including multiplier.
                    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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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

Generated by m2html © 2003