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 で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!