2011-01-30 14 views
27

Mam zmienne w MATLAB, sprawdziłem ich klasy przy użyciu class(), ale chcę również poznać rozmiar, który biorą w pamięci. Dokładniej, wiem, że są one podwójnego typu i chcę się upewnić, że są 32-bitowe podwójne, a nie 64-bitowe.Jak poznać rozmiar zmiennej w MATLAB

Wersja MATLAB, której używam, to R2009b.

Odpowiedz

29

Możesz użyć whos, aby uzyskać tablicę struktur, które opisują, między innymi, rozmiar w bajtach każdej zmiennej.

Należy pamiętać, że podwójne jest z definicji 64-bitowe!

27

Napisałem prostą funkcję wygody, aby poradzić sobie z tym problemem. Użycie jest:

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

biegnę R2007a, więc problem z obiektów Javy nie rozmiary powracający mogły zostać usunięte w kolejnych wydaniach. Oto kod:

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 
+0

bardzo przydatna funkcja – Saikat

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

To daje wielkość w megabajtach, np MB = 123,78

1

Starałem się poprawić prostą funkcję „MatlabSorter za” obsłużyć ten problem. Wykorzystanie jest wciąż ten sam:

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

dodatki:

1.you mogą Państwo, które rodzaj zamian szukać - B, KB, MB, TB lub pb

2.you można dostać wynik jako zmienną bez drukowania na ekranie

Oto kod:

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 
Powiązane problemy