Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the size of a variable in MATLAB

Tags:

size

matlab

I have variables in MATLAB, I've checked their class using class() but I also want to know the size that they take in the memory. More accurately, I know that they are of double type, and I want to make sure that they are 32-bit double and not 64-bit.

The version of the MATLAB I'm using is R2009b.

like image 838
SIMEL Avatar asked Jan 30 '11 21:01

SIMEL


People also ask

How do you find the size of a variable in MATLAB?

size (MATLAB Functions) d = size(X) returns the sizes of each dimension of array X in a vector d with ndims(X) elements. [m,n] = size(X) returns the size of matrix X in separate variables m and n . m = size(X,dim) returns the size of the dimension of X specified by scalar dim .

How do you find the size of a variable?

The size of a variable depends on its type, and C++ has a very convenient operator called sizeof that tells you the size in bytes of a variable or a type. The usage of sizeof is simple. To determine the size of an integer, you invoke sizeof with parameter int (the type) as demonstrated by Listing 3.5.

How do you find the size of a vector in MATLAB?

Description. sz = size( A ) returns a row vector whose elements are the lengths of the corresponding dimensions of A . For example, if A is a 3-by-4 matrix, then size(A) returns the vector [3 4] .

How do you find the length of a matrix in MATLAB?

Description. L = length( X ) returns the length of the largest array dimension in X . For vectors, the length is simply the number of elements. For arrays with more dimensions, the length is max(size(X)) .


4 Answers

I wrote a simple convenience function to handle just this problem. Usage is:

>> x = ones(1000);
>> ByteSize(x)
7.63 Mb

I run R2007a, so the problem of Java objects not returning sizes may have been fixed in subsequent releases. Here's the code:

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
like image 127
MatlabSorter Avatar answered Oct 06 '22 04:10

MatlabSorter


You can use whos to obtain an array of structures that describe, amongst other things, the size in bytes of each variable.

Note that a double is, by definition, 64-bit!

like image 22
Oliver Charlesworth Avatar answered Oct 06 '22 04:10

Oliver Charlesworth


dt=whos('VARIABLE_YOU_CARE_ABOUT'); MB=dt.bytes*9.53674e-7;  

This will give you the size in megabytes, for example MB=123.78

like image 21
Ben Avatar answered Oct 06 '22 05:10

Ben


I tried to enhance ' MatlabSorter's ' simple function to handle this problem. Usage is still the same:

>> x = ones(1000);
>> getByteSize(x)
7.63 mb

additions :

1.you can state which type of return you seek for - b, kb, mb, tb or pb

2.you can get the result as a variable without printing it on the screen

Here's the code:

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
like image 42
Doga Siyli Avatar answered Oct 06 '22 03:10

Doga Siyli