Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
workspace bytes to MB

Subject: workspace bytes to MB

From: Oleg Komarov

Date: 30 Dec, 2009 16:28:04

Message: 1 of 12

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

Subject: workspace bytes to MB

From: Matt J

Date: 30 Dec, 2009 20:02:03

Message: 2 of 12

"Oleg Komarov" <oleg.komarov@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;

Subject: workspace bytes to MB

From: us

Date: 30 Dec, 2009 21:07:04

Message: 3 of 12

"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

Subject: workspace bytes to MB

From: Matt J

Date: 30 Dec, 2009 21:42:02

Message: 4 of 12

"us " <us@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.)

Subject: workspace bytes to MB

From: us

Date: 30 Dec, 2009 22:03:02

Message: 5 of 12

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hhghfa$j9e$1@fred.mathworks.com>...
> "us " <us@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.)

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

Subject: workspace bytes to MB

From: Matt J

Date: 30 Dec, 2009 22:16:02

Message: 6 of 12

"us " <us@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) :-)

Subject: workspace bytes to MB

From: Oleg Komarov

Date: 30 Dec, 2009 22:21:03

Message: 7 of 12

> 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

Subject: workspace bytes to MB

From: Oleg Komarov

Date: 31 Dec, 2009 11:43:04

Message: 8 of 12

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

Subject: workspace bytes to MB

From: Oleg Komarov

Date: 31 Dec, 2009 13:28:03

Message: 9 of 12

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

Subject: workspace bytes to MB

From: us

Date: 31 Dec, 2009 14:50:20

Message: 10 of 12

"Oleg Komarov" <oleg.komarov@hotmail.it> wrote in message <hhi8t3$2sj$1@fred.mathworks.com>...
> 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

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

us

Subject: workspace bytes to MB

From: Oleg Komarov

Date: 31 Dec, 2009 15:04:03

Message: 11 of 12

> 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

Subject: workspace bytes to MB

From: Yair Altman

Date: 3 Jan, 2010 00:38:03

Message: 12 of 12

"Oleg Komarov" <oleg.komarov@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.
> Do you know of any way to accomplish this? Maybe Yair has some tricks up in his sleeve...
>
> Thanks and happy new year!
>
> Oleg


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

Enjoy :-)

Yair Altman
http://UndocumentedMatlab.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us