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

Lane
Lane 2015년 9월 18일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Kelly Kearney
Kelly Kearney 2015년 9월 18일

2 개 추천

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

Jack Lee
Jack Lee 2016년 3월 3일
편집: Jack Lee 2016년 3월 3일
Thank you very much!
Stephen23
Stephen23 2017년 11월 22일
편집: Stephen23 2017년 11월 22일
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 2018년 4월 18일
just replace "cellstr2" with "cellstr".

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2015년 4월 30일

댓글:

2018년 4월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by