How to get Data type of workspace loaded variables and how add that datatypes using script?
이전 댓글 표시
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
Steven Lord
2021년 4월 15일
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
2021년 4월 16일
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
2021년 4월 16일
편집: Walter Roberson
2021년 4월 16일
Steven Lord
2021년 4월 16일
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
2021년 4월 16일
답변 (1개)
Xingwang Yong
2021년 4월 13일
1 개 추천
s=whos;
numBytes = sum([s.bytes]);
댓글 수: 14
Dhinesh K
2021년 4월 13일
Stephen23
2021년 4월 13일
Do not multiply by 8.
Dhinesh K
2021년 4월 13일
Dhinesh K
2021년 4월 13일
Xingwang Yong
2021년 4월 13일
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
2021년 4월 13일
Dhinesh K
2021년 4월 13일
Dhinesh K
2021년 4월 15일
Stephen23
2021년 4월 15일
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
Stephen23
2021년 4월 15일
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
2021년 4월 15일
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!