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

72 visualizaciones (últimos 30 días)
FM
FM el 27 de Abr. de 2022
Editada: FM el 27 de Abr. de 2022
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?

Respuesta aceptada

Matt J
Matt J el 27 de Abr. de 2022
Editada: Matt J el 27 de Abr. de 2022
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 comentario
FM
FM el 27 de Abr. de 2022
Editada: FM el 27 de Abr. de 2022
Yes! Exactly! Thanks!
I put it in my MATLAB folder (where all utility functions reside), referring to it as StrucFldBytes.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by