Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

workspace bytes to MB

4 views
Skip to first unread message

Oleg Komarov

unread,
Dec 30, 2009, 11:28:04 AM12/30/09
to
Hi to everyone,
I'd like to display the memory used by the variables in the workspace pane in MB instead of bytes.
Do you know of any way to accomplish this? Maybe Yair has some tricks up in his sleeve...

Thanks and happy new year!

Oleg

Matt J

unread,
Dec 30, 2009, 3:02:03 PM12/30/09
to
"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message <hhfv2k$27s$1...@fred.mathworks.com>...

> Hi to everyone,
> I'd like to display the memory used by the variables in the workspace pane in MB instead of bytes.
=================

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;

us

unread,
Dec 30, 2009, 4:07:04 PM12/30/09
to
"Matt J "

> 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

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

Matt J

unread,
Dec 30, 2009, 4:42:02 PM12/30/09
to
"us " <u...@neurol.unizh.ch> wrote in message <hhgfdo$baf$1...@fred.mathworks.com>...

> "Matt J "
> > 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
>
> another, slightly more versatile approach...
> - note the enhanced possibility to input a var


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.)

us

unread,
Dec 30, 2009, 5:03:02 PM12/30/09
to
"Matt J " <mattja...@THISieee.spam> wrote in message <hhghfa$j9e$1...@fred.mathworks.com>...

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

Matt J

unread,
Dec 30, 2009, 5:16:02 PM12/30/09
to
"us " <u...@neurol.unizh.ch> wrote in message <hhgimm$62h$1...@fred.mathworks.com>...

> % 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 Komarov

unread,
Dec 30, 2009, 5:21:03 PM12/30/09
to
> but: this is probably NOT what the OP is looking for...
> us
Definitely not...I'm refering to the interface itself. The workspace pane has the option to display the size as a detail of the variable itself without the use of any command.
Since this detail (at least from the R2008a ver) is expressed in bytes it loses meaning when the variables grow big.

Oleg

Oleg Komarov

unread,
Dec 31, 2009, 6:43:04 AM12/31/09
to
Thanks to Matt J and us for the replies.
I've submitted and enhacement request and in the meantime i'll implement the suggested solutions.

Oleg

Oleg Komarov

unread,
Dec 31, 2009, 8:28:03 AM12/31/09
to
And this is the solution proposed by the support:

% 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

us

unread,
Dec 31, 2009, 9:50:20 AM12/31/09
to
"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message <hhi8t3$2sj$1...@fred.mathworks.com>...

wow! what a smart solution - yet, still NOT what you were asking for...

us

Oleg Komarov

unread,
Dec 31, 2009, 10:04:03 AM12/31/09
to
> wow! what a smart solution - yet, still NOT what you were asking for...
>
> us
Yes i know but they've submitted an enhancement request to the developers.
Anyway this solution (and the one you proposed) is much faster than googling '76537543 bytes to MB' :)

Oleg

Yair Altman

unread,
Jan 2, 2010, 7:38:03 PM1/2/10
to
"Oleg Komarov" <oleg.k...@hotmail.it> wrote in message <hhfv2k$27s$1...@fred.mathworks.com>...


I couldn't resist the challenge...
http://UndocumentedMatlab.com/blog/customizing-matlabs-workspace-table/

Enjoy :-)

Yair Altman
http://UndocumentedMatlab.com

0 new messages