Home > atmlab > handy > nbytes2string.m

nbytes2string

PURPOSE ^

nbytes2string Give human-readable representation of no. of bytes

SYNOPSIS ^

function s = nbytes2string(n, varargin)

DESCRIPTION ^

 nbytes2string Give human-readable representation of no. of bytes

 IN

   n       integer     number of bytes
   SI      logical     if set and true, use SI (10^n) instead of 2^m

 OUT

   string with human-readable format

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

nbytes2string.m

SOURCE CODE ^

0001 function s = nbytes2string(n, varargin)
0002 
0003 % nbytes2string Give human-readable representation of no. of bytes
0004 %
0005 % IN
0006 %
0007 %   n       integer     number of bytes
0008 %   SI      logical     if set and true, use SI (10^n) instead of 2^m
0009 %
0010 % OUT
0011 %
0012 %   string with human-readable format
0013 
0014 SI = optargs(varargin, {false});
0015 
0016 binary_names = {'bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'};
0017 SI_names = {'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'};
0018 
0019 if SI
0020     names = SI_names;
0021     factor = 2.^10;
0022 else
0023     names = binary_names;
0024     factor = 10.^3;
0025 end
0026 
0027 i = 1;
0028 
0029 while n > factor && i < length(names)
0030     n = n / factor;
0031     i = i + 1;
0032 end
0033 
0034 if i==1
0035     s = sprintf('%d %s', n, names{i});
0036 else
0037     s = sprintf('%.3f %s', n, names{i});
0038 end
0039 
0040 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005