我在MATLAB中有变量,我已经检查了它们的类,class()但我也想知道它们在内存中的大小.更准确地说,我知道它们是双重类型,我想确保它们是32位双精度而不是64位.
我正在使用的MATLAB版本是R2009b.
Mat*_*ter 31
我写了一个简单的便利函数来处理这个问题.用法是:
>> x = ones(1000);
>> ByteSize(x)
7.63 Mb
Run Code Online (Sandbox Code Playgroud)
我运行R2007a,因此Java对象不返回大小的问题可能已在后续版本中修复.这是代码:
function 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');
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
Run Code Online (Sandbox Code Playgroud)
小智 11
dt=whos('VARIABLE_YOU_CARE_ABOUT'); MB=dt.bytes*9.53674e-7;
Run Code Online (Sandbox Code Playgroud)
这将为您提供以兆字节为单位的大小,例如MB = 123.78