How to get Data type of workspace loaded variables and how add that datatypes using script?
Afficher commentaires plus anciens
I have a .m files in that file many variales are created and loaded in workspace also but i want to add all the datatypes as bytes. can you tell me how to do?
Like uint8 as 1byte
uint16 as 2 byte so totally 3 bytes.
Thanks in advance.
6 commentaires
Steven Lord
le 15 Avr 2021
How are you planning to use this information if you are able to obtain it? Counting how much memory is actually used can be a tricky question due to optimizations like copy-on-write.
A = rand(1, 10);
B = A; % This is not a copy yet. It references the same memory as A.
B(1) = 1; % Now it's a copy.
Steven Lord
le 16 Avr 2021
That tells me what you want to do. But why do you need this information and do you care that your approach may not give you an accurate measurement of how much memory has actually been allocated?
Dhinesh K
le 16 Avr 2021
Modifié(e) : Walter Roberson
le 16 Avr 2021
Steven Lord
le 16 Avr 2021
Okay, let's say for sake of argument that the sizes of the various properties added up to 420 bytes.
Why is that important? How are you going to use that information?
Dhinesh K
le 16 Avr 2021
Réponses (1)
Xingwang Yong
le 13 Avr 2021
1 vote
s=whos;
numBytes = sum([s.bytes]);
14 commentaires
Dhinesh K
le 13 Avr 2021
Stephen23
le 13 Avr 2021
Do not multiply by 8.
Dhinesh K
le 13 Avr 2021
Dhinesh K
le 13 Avr 2021
Xingwang Yong
le 13 Avr 2021
According to doc of uint8, it only takes 1 byte. And on my R2020a, it indeed takes 1 byte.
clear
var1=uint8(1);
whos
@Dhinesh K: it is unclear what the problem is. 8 bits is one byte.
"but matlab takes as default 8 bytes."
It is unclear how that is related to your question. The default variable class (double) uses exactly eight bytes:
x = 1; % double = 64 bits = 8 bytes
y = uint16(2); % 16 bits = 2 bytes
whos
and whos correctly returns the number of bytes for each variable. It is not clear what you expect to happen.
Dhinesh K
le 13 Avr 2021
Dhinesh K
le 13 Avr 2021
Dhinesh K
le 15 Avr 2021
Stephen23
le 15 Avr 2021
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
Stephen23
le 15 Avr 2021
You are only counting the number of bytes in the object handle. If you want to count the bytes in all attributes/properties of the object itself, then try one of the methods outlined in the links I showed you.
Dhinesh K
le 15 Avr 2021
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!