Apply "whos" to fields of a struct to get their sizes?

조회 수: 73 (최근 30일)
FM
FM 2022년 4월 27일
편집: FM 2022년 4월 27일
The command "whos" shows the sizes (in bytes) [of the data objects/variables in the base workspace. Is there a way to get that information for the fields of a struct?

채택된 답변

Matt J
Matt J 2022년 4월 27일
편집: Matt J 2022년 4월 27일
Something like this, perhaps?
a=rand(100); b=1:4;
S.a=a; S.b=b;
Sbytes=whosstruct(S);
whos a b
Name Size Bytes Class Attributes a 100x100 80000 double b 1x4 32 double
Sbytes
Sbytes = struct with fields:
a: 80000 b: 32
function Sbytes=whosstruct(S)
for fld=fieldnames(S)'
val=S.(fld{1});
tmp=whos('val');
Sbytes.(fld{1})=tmp.bytes;
end
end
  댓글 수: 1
FM
FM 2022년 4월 27일
편집: FM 2022년 4월 27일
Yes! Exactly! Thanks!
I put it in my MATLAB folder (where all utility functions reside), referring to it as StrucFldBytes.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by