-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathByteSize.m
More file actions
executable file
·36 lines (32 loc) · 1008 Bytes
/
ByteSize.m
File metadata and controls
executable file
·36 lines (32 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function str = ByteSize(in, fid)
% BYTESIZE writes the memory usage of the provide variable to the given file
% identifier. Output is written to screen if fid is 1, empty or not provided.
if nargin == 1 || isempty(fid)
fid = 1;
end
s = whos('in');
str = Bytes2str(s.bytes);
%fprintf(fid,[Bytes2str(s.bytes) '\n']);
end
function str = Bytes2str(NumBytes)
% BYTES2STR Private function to take integer bytes and convert it to
% scale-appropriate size.
scale = floor(log(NumBytes)/log(1024));
switch scale
case 0
str = [sprintf('%.0f',NumBytes) ' b'];
case 1
str = [sprintf('%.2f',NumBytes/(1024)) ' kb'];
case 2
str = [sprintf('%.2f',NumBytes/(1024^2)) ' Mb'];
case 3
str = [sprintf('%.2f',NumBytes/(1024^3)) ' Gb'];
case 4
str = [sprintf('%.2f',NumBytes/(1024^4)) ' Tb'];
case -inf
% Size occasionally returned as zero (eg some Java objects).
str = 'Not Available';
otherwise
str = 'Over a petabyte!!!';
end
end