Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MATLAB中知道变量的大小_Matlab_Size - Fatal编程技术网

如何在MATLAB中知道变量的大小

如何在MATLAB中知道变量的大小,matlab,size,Matlab,Size,我在MATLAB中有变量,我用class()检查了它们的类,但我也想知道它们在内存中的大小。更准确地说,我知道它们是双精度类型,我想确保它们是32位双精度而不是64位 我使用的MATLAB版本是R2009b。您可以使用它来获得一组结构,其中包括描述每个变量的字节大小 注意,根据定义,double是64位的 我编写了一个简单的便利函数来处理这个问题。用途是: >> x = ones(1000); >> ByteSize(x) 7.63 Mb 我运行R2007a,因此Jav

我在MATLAB中有变量,我用
class()
检查了它们的类,但我也想知道它们在内存中的大小。更准确地说,我知道它们是双精度类型,我想确保它们是32位双精度而不是64位

我使用的MATLAB版本是R2009b。

您可以使用它来获得一组结构,其中包括描述每个变量的字节大小


注意,根据定义,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
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

这将为您提供以MB为单位的大小,例如MB=123.78

我尝试增强“MatlabSorter”的简单功能来处理此问题。用法仍然相同:

>> x = ones(1000);
>> getByteSize(x)
7.63 mb
补充:

1.您可以说明您寻求的返回类型-b、kb、mb、tb或pb

2.无需在屏幕上打印即可将结果作为变量获得

代码如下:

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
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
函数b=getByteSize(变量,返回类型,fid)
%getByteSize将所提供变量(变量)的mem.usage返回给给定文件
%标识符。
%如果未说明,则根据字节大小有意义地分配returnType
%如果fid为1、为空或未提供,则输出写入屏幕。
s=whos(“变量”);
b=s字节;
如果nargin==1 | |为空(returnType)
刻度=地板(对数(b)/对数(1024));
开关秤
案例0
returnType='byte';
案例1
returnType='kb';
案例2
returnType='mb';
案例3
returnType='gb';
案例4
returnType='tb';
case-inf
%大小偶尔返回为零(例如一些Java对象)。
returnType='byte';
警告('大小偶尔返回为零(例如一些Java对象)。假定为字节');
否则
returnType='petabytes';
警告(“假定超过1024 PB.PB”);
结束
结束
开关返回式
大小写{b',byte',bytes}
b=s字节;
大小写{'kb','kbs','kb','kb'}
b=b/1024;
大小写{'mb','mbs','mb','mb'}
b=b/1024^2;
大小写{gb',gbs',gb',gb}
b=b/1024^3;
大小写{'tb','tbs','tb','tb'}
b=b/1024^4;
大小写{pb',pbs',pb',pb}
b=b/1024^5;
否则
returnType='bytes';
结束
如果nargin 2&&~为空(fid)&&fid>2
尝试
fprintf(fid,[num2str(b)'returnType'\n']);
抓住
警告(['fid('num2str(fid)')无法编辑。因此,输出将写入屏幕上。');
fprintf(1,[num2str(b)'returnType'\n']);
结束
结束
结束