我有MATLAB中的变量,我已经检查他们的类使用class()
,但我也想知道他们在内存中的大小。更准确地说,我知道它们是双重类型的,我想确保它们是32位双精度而不是64位。如何知道在MATLAB中的变量的大小
我使用的MATLAB版本是R2009b。
我有MATLAB中的变量,我已经检查他们的类使用class()
,但我也想知道他们在内存中的大小。更准确地说,我知道它们是双重类型的,我想确保它们是32位双精度而不是64位。如何知道在MATLAB中的变量的大小
我使用的MATLAB版本是R2009b。
您可以使用whos来获取一组结构,其中描述了各个变量的大小(以字节为单位)。
请注意,根据定义,double是64位!
我写了一个简单的便利函数来处理这个问题。用法是:
>> x = ones(1000);
>> ByteSize(x)
7.63 Mb
我跑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
dt=whos('VARIABLE_YOU_CARE_ABOUT'); MB=dt.bytes*9.53674e-7;
这会给你以MB为单位的大小,例如MB = 123.78
我试图提升“MatlabSorter的”简单的函数来处理这个问题。用法仍然是相同的:
>> x = ones(1000);
>> getByteSize(x)
7.63 mb
补充:
1.you能说出你寻求哪种类型的回报 - B,KB,MB,TB或PB
2.you可以得到结果作为一个变量,但不打印屏幕
这里上的代码:
function b = getByteSize(theVariable, returnType, fid)
% getByteSize returns the mem.usage of the provided variable(theVariable) to the given file
% identifier.
% returnType is assigned meaningfully according to the byte size if not stated
% Output is written to screen if fid is 1, empty or not provided.
s = whos('theVariable');
b = s.bytes;
if nargin == 1 || isempty(returnType)
scale = floor(log(b)/log(1024));
switch scale
case 0
returnType = 'byte';
case 1
returnType = 'kb';
case 2
returnType = 'mb';
case 3
returnType = 'gb';
case 4
returnType = 'tb';
case -inf
% Size occasionally returned as zero (eg some Java objects).
returnType = 'byte';
warning('Size occasionally returned as zero (eg some Java objects). Bytes assumed');
otherwise
returnType = 'petabytes';
warning('Over 1024 petabyte. petabytes assumed');
end
end
switch returnType
case {'b','byte','bytes'}
b = s.bytes;
case {'kb','kbs','kilobyte','kilobytes'}
b = b/1024;
case {'mb','mbs','megabyte','megabytes'}
b = b/1024^2;
case {'gb','gbs','gigabyte','gigabytes'}
b = b/1024^3;
case {'tb','tbs','terabyte','terabytes'}
b = b/1024^4;
case {'pb','pbs','petabyte','petabytes'}
b = b/1024^5;
otherwise
returnType = 'bytes';
end
if nargin <= 2 || isempty(fid) || fid == 1
fprintf(1,[num2str(b) ' ' returnType '\n']);
elseif nargin > 2 && ~isempty(fid) && fid > 2
try
fprintf(fid,[num2str(b) ' ' returnType '\n']);
catch
warning(['fid(' num2str(fid) ') could not be edited. Hence the output will be written on the screen.']);
fprintf(1,[num2str(b) ' ' returnType '\n']);
end
end
end
非常有用的功能 – Saikat 2013-08-06 19:30:10