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

67 ビュー (過去 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 Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by