Thanks and happy new year!
Oleg
I'd like to know even just how to display them in bytes. My Workspace pane only displays Name, Value, Min, & Max
Anyway, it may not be quite what you're after but the function below does something similar
Example:
X=rand(3000);
>> meg X
ans =
68.6646
function mem=meg(varargin)
%
%Issuing the command MEG returns the number of megabytes of used memory
%as tracked by WHOS().
%
%MEG X Y will display the memory in MB consumed (according to whos) by variables X %and Y
if nargin==0
cmdstr=['whos;'];
s=evalin('caller', cmdstr) ;
else
in=['''' varargin{1} ''''];
numargs=length(varargin);
if numargs>1
for ii=2:numargs
in= [in ',' '''' varargin{ii} ''''];
end
end
cmdstr=['whos(' in ');'];
s=evalin('caller', cmdstr) ;
end
if length(s)<1, mem=0; return; end
c=cell(1,length(s));
[c{:}]=deal(s.bytes);
c=vertcat(c{:});
mem=sum(c)/2^20;
another, slightly more versatile approach...
- note the enhanced possibility to input a var
%SYNTAX
%---------------------------------
% MEB x y* 'zz';
% MEB('x',zzz,'y*');
%
function r=meb(varargin)
mb=2^20;
in='';
if nargin
arg=varargin;
ix=~cellfun(@ischar,arg);
in=arrayfun(@inputname,find(ix),'uni',false);
arg(ix)=in;
arg=unique(arg);
in=sprintf('''%s'',',arg{:});
in(end)='';
end
% get/convert var name/size
com=['whos(',in,');'];
s=evalin('base',com);
sn={s.name}.';
sb=cat(1,s.bytes)./mb;
r=[sn,num2cell(sb)].';
% beautify output
tl=1+max(cellfun('length',sn));
bl=1+max(ceil(log10(sb)));
fm=sprintf('%%-%ds %%%d.2f MB\n',tl,bl);
r=sprintf(fm,r{:});
disp(r);
if ~nargout
clear r;
end
end
but: this is probably NOT what the OP is looking for...
us
Sorry, I'm not seeing the increased versatility you mention, only a difference in the format of the output. (My version also took vars as input.)
an example
x=rand(1000);
y=x;
yy=x(1:500,1:500);
% MEB
meb('x','y*'); % <- works as expected...
meb(x,yy,'y'); % <- works as well...
% MEG
meg('x','y*') % <- works as expected...
meg(x,yy,'y') % <- does NOT work...
just a thought
us
> % MEB
> meb('x','y*'); % <- works as expected...
> meb(x,yy,'y'); % <- works as well...
> % MEG
> meg('x','y*') % <- works as expected...
> meg(x,yy,'y') % <- does NOT work...
>
Ah. Okay.
Anyway, sorry we're not being of more help to you, Oleg (except perhaps by continually bumping this thread) :-)
Oleg
Oleg
% WSSIZE Display size of variables in the base workspace using B, KB, MB and GB
function wssize
% Obtain base workspace contents
ws_contents = evalin('base', 'whos');
% Loop through contents and display size on screen
for i = 1:length(ws_contents)
cur_size = ws_contents(i).bytes;
if cur_size > 1024^3;
fprintf('%-15s: %8.3f GB\n', ws_contents(i).name, cur_size/1024^3);
elseif cur_size > 1024^2;
fprintf('%-15s: %8.3f MB\n', ws_contents(i).name, cur_size/1024^2);
elseif cur_size > 1024;
fprintf('%-15s: %8.3f KB\n', ws_contents(i).name, cur_size/1024);
else
fprintf('%-15s: %8.3f B\n', ws_contents(i).name, cur_size);
end
end
end
Oleg
wow! what a smart solution - yet, still NOT what you were asking for...
us
Oleg
I couldn't resist the challenge...
http://UndocumentedMatlab.com/blog/customizing-matlabs-workspace-table/
Enjoy :-)
Yair Altman
http://UndocumentedMatlab.com