Code covered by the BSD License  

Highlights from
Analog Filter Design Toolbox

image thumbnail
from Analog Filter Design Toolbox by James Squire
GUI to design and simulate active (opamp) LP and HP Bessel, Butter, Cheby, and Elliptic filters.

Utility_EngOutput(num, arg1,arg2,low,high)
function [argOut1, argOut2] = Utility_EngOutput(num, arg1,arg2,low,high)
% Utility_EngOutput is a subfile of the AnalogFilter GUI collection
% Examples:
% EngOutput(123456) returns '123.4k' (by default uses 4 sig digits)
% EngOutput(123456,3) returns '123k' (sig digits specified)
% EngOutput(12343456,'Ohms') returns '123.4kOhms' 
% EngOutput(123456,3,'Ohms') returns '123kOhms' 
% EngOutput(1,'F',4,-12,-6) limits the response between pF and uF (i.e. '1.000e6uF')
% [sMan, nExp] = EngOutput(1e-6,'F',4,-12,-6) returns sMan='1' and nExp=-6

% James C. Squire, Assistant Professor, Virginia Military Institute
% ver 1.0

% Constants
DEFAULTSIGDIGITS = 4;
SUFFIXLIST = 'afpnum kMGTPE'; % atto, femto, pico, nano, etc.
INEUTRAL = 7;                 % index of 1's place in above list
% if passed a null argument
if isempty(num)
    argOut1='';
    argOut2=[];
    return
end
% check for infinities
if isinf(num)
    if num==inf
        argOut1 = 'Infinity';
    else
        argOut1 = -'Infinity';
    end
    argOut2 = [];
    return
end   
% sort out input form
nSigDigits = DEFAULTSIGDIGITS;
sUserSuffix = '';
if nargin > 1
    if ischar(arg1)
        sUserSuffix = arg1;
    else
        nSigDigits = arg1;
    end
end
if nargin==3
    if ischar(arg2)
        sUserSuffix = arg2;
    else
        nSigDigits = arg2;
    end
end 
if isempty(nSigDigits) || (nSigDigits ~= round(nSigDigits)) || nSigDigits<0 || nSigDigits>12
    warning(['Incorrect form of significant digits in ' mfilename]);
    nSigDigits = DEFAULTSIGDIGITS;
end
if nargin<4
    low = -inf;
    high = inf;
end
% check for zeros
if num==0
    if nargout <2
        argOut1 = ['0 ' sUserSuffix];
    else
        argOut1 = '0';
        argOut2 = 0;
    end
    return
end
% find the exponent string
maxI = min((length(SUFFIXLIST) - INEUTRAL), high/3);
minI = max(1-INEUTRAL, low/3);
iSuffix = floor(log10(abs(num))/3);
if iSuffix > maxI, iSuffix = maxI; end
if iSuffix < minI, iSuffix = minI; end
sExp = SUFFIXLIST(iSuffix + INEUTRAL);
if sExp == ' ', sExp = ''; end % if no exponent, remove the space
% find the mantissa string
mantissa = num/10^(3*iSuffix);
sForm = ['%0.' num2str(nSigDigits) 'g'];
sMantissa = sprintf(sForm, mantissa);
% assemble
if nargout <2
    argOut1 = [sMantissa ' ' sExp sUserSuffix];
else
    argOut1 = sMantissa;
    argOut2 = iSuffix*3;
end

Contact us at files@mathworks.com