function b = bytes(whs) % T.Hilmer U.H. % handy way of checking your memory usage % % function b = bytes(whs) % where "whs" is a variable in your workspace % OR % function b = bytes % (same as bytes(whos) if nargin == 0 whs = evalin('caller','whos'); end if nargin == 1 && ~isstruct(whs) || ~isfield(whs,'bytes') whs = whos;% works because there are no other variables in function workspace end b = sum([whs.bytes]); % No output, display answer if ~nargout if b >= 1E6 b = b*1E-6; units = 'MB'; elseif b >= 1E3 b = b*1E-3; units = 'KB'; else units = 'B'; end disp([sprintf('%.0f ',b) units]) clear b end