Display the sizes of 4D arrays?

47 views (last 30 days)
Could matlab display the sizes of arrays with dimensions>=4?
I have many 4D arrays and would like to see their sizes during debug.It is very painful to type size(someLongName) everytime.
Is there a way to make matlab display their sizes (either in "whos" or when moving the mouse over the variable, i.e. in the pop-up description box you usually see)?
Name Size Bytes Class Attributes
ans 1x4 32 double
someLongName 4-D 40000 double
Thanks a lot!
  1 Comment
Lane
Lane on 18 Sep 2015
I too would very much like to see 4-D array sizes displayed using the whos command. I think it's the same issue, but I have 4D arrays that are fields of a structure, and when I type that structure's name I have to do separate commands to see the size of each 4-D field.

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 18 Sep 2015
I wrote the following little function as a slightly altered version of whos. It lists all dimensions, and also converts the Bytes field into a human-readable format. Example in action:
>> a = rand(1,2);
>> b = rand(3,4,5);
>> c = rand(2,3,4,5,6);
>> whos
Name Size Bytes Class Attributes
a 1x2 16 double
b 3x4x5 480 double
c 5-D 5760 double
>> whosh
Name Size Bytes Class
a 1x2 16B double
b 3x4x5 480B double
c 2x3x4x5x6 5K double
The function:
function whosh(varargin)
if nargin < 1
S = evalin('base', 'whos');
else
argstr = sprintf('''%s'',', varargin{:});
argstr = argstr(1:end-1);
expression = ['whos(' argstr ')'];
S = evalin('base', expression);
end
% Bytes string
gig = [S.bytes] > 1024^3;
meg = [S.bytes] > 1024^2 & ~gig;
kil = [S.bytes] > 1024 & ~meg & ~gig;
bytes = [S.bytes]';
bytes(gig) = [S(gig).bytes] ./ 1024^3;
bytes(meg) = [S(meg).bytes] ./ 1024^2;
bytes(kil) = [S(kil).bytes] ./ 1024;
suffix = cell(size(bytes));
[suffix{:}] = deal('B');
[suffix{gig}] = deal('G');
[suffix{meg}] = deal('M');
[suffix{kil}] = deal('K');
bytestr = cellfun(@(a,b) sprintf('%d%s', a, b), num2cell(floor(bytes)), suffix, 'uni', 0);
% Size string
sizestr = arrayfun(@(X) sprintf('%dx', X.size), S, 'uni', 0);
sizestr = cellfun(@(x) x(1:end-1), sizestr, 'uni', 0);
xloc = strfind(sizestr, 'x');
xloc = cellfun(@(x) x(1), xloc);
xlocmax = max(xloc);
npad = num2cell(xlocmax - xloc);
sizestr = cellfun(@(str,x) [repmat(' ', 1,x) str], sizestr, npad, 'uni', 0);
% Format strings
namecol = strvcat('Name', S.name);
sizecol = strvcat('Size', sizestr{:});
bytescol = strvcat('Bytes', bytestr{:});
bytescol(2:end,:) = strjust(bytescol(2:end,:), 'right');
classcol = strvcat('Class', S.class);
data = [cellstr2(namecol) cellstr2(sizecol) cellstr2(bytescol) cellstr2(classcol)];
fprintf('%s %s %s %s\n\n', data{1,:});
temp = data(2:end,:)';
fprintf('%s %s %s %s\n', temp{:});
  3 Comments
Stephen23
Stephen23 on 22 Nov 2017
Edited: Stephen23 on 22 Nov 2017
roberto ivan perez luna's "Answer" moved here:
hi, i get this error when i am using your script on my 4-D double variables array. so i hope you can someday fix it, if you want to, since this is not your fault and every user help is more concrete than the official matlab issue of not upgrading the function of a command, instead of creating another one and so no, and so on.
>> whosh
Undefined function or variable 'cellstr2'.
Error in whosh (line 51)
data = [cellstr2(namecol) cellstr2(sizecol) cellstr2(bytescol) cellstr2(classcol)];
Jonathan Blanchette
Jonathan Blanchette on 18 Apr 2018
just replace "cellstr2" with "cellstr".

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!