How to get Data type of workspace loaded variables and how add that datatypes using script?
Show older comments
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 Comments
Steven Lord
on 15 Apr 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
on 16 Apr 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
on 16 Apr 2021
Edited: Walter Roberson
on 16 Apr 2021
Steven Lord
on 16 Apr 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
on 16 Apr 2021
Answers (1)
Xingwang Yong
on 13 Apr 2021
1 vote
s=whos;
numBytes = sum([s.bytes]);
14 Comments
Dhinesh K
on 13 Apr 2021
Stephen23
on 13 Apr 2021
Do not multiply by 8.
Dhinesh K
on 13 Apr 2021
Dhinesh K
on 13 Apr 2021
Xingwang Yong
on 13 Apr 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
on 13 Apr 2021
Dhinesh K
on 13 Apr 2021
Dhinesh K
on 15 Apr 2021
Stephen23
on 15 Apr 2021
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
Stephen23
on 15 Apr 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
on 15 Apr 2021
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!