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 件のコメント

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.
Dhinesh K
Dhinesh K 2021 年 4 月 16 日
編集済み: Dhinesh K 2021 年 4 月 16 日
@Steven Lord My planning is first i will extract all the datatype from .m file and store one variable. then extract uint8, uint16, uint32 separately for all datatypes. if unit8, 5 variable means 5*1 because uint8 is 1 byte. so 5 variables means 5 bytes. finally need to add all the values, then we will get exact output.
Steven Lord
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
Dhinesh K 2021 年 4 月 16 日
編集済み: Walter Roberson 2021 年 4 月 16 日
@Steven Lord yes. I want to calculate howmuch memory allocated for the below mentioned variable. the below mentioned variable is Light this variable datatype is uint8, Here i want to calculate memory of this Light variable using scripts.
Like wise i have N no of variables in my .m file, so i have to calculate those variables memory.
Example variable:
Light = Simulink.Signal;
Light.CoderInfo.StorageClass = 'ImportedExtern';
Light.CoderInfo.Alias = '';
Light.CoderInfo.Alignment = -1;
Light.CoderInfo.CustomStorageClass = 'Default';
Light.Description = '';
Light.DataType = 'uint8';
Light.Min =0;
Light.Max =1;
Light.DocUnits = '';
Light.Dimensions = -1;
Light.DimensionsMode = 'Auto';
Light.Complexity = 'Auto';
Light.SampleTime = 0.01;
Light.SamplingMode = 'Auto';
Light.InitialValue = '';
Steven Lord
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
Dhinesh K 2021 年 4 月 16 日
I need that info for my project. thats why

サインインしてコメントする。

回答 (1 件)

Xingwang Yong
Xingwang Yong 2021 年 4 月 13 日

1 投票

s=whos;
numBytes = sum([s.bytes]);

14 件のコメント

Dhinesh K
Dhinesh K 2021 年 4 月 13 日
Thanks for your answer@Xingwang Yong , But .bytes consists of each variable as 8bytes. so cannot calculate approximate bytes.
ex: If i have 10 varible with uint8 datatype means, my calculated bytes should be (10*1)=10bytes. but using this code i got (10*8)=80.
could you please teach me how to proceed.
Stephen23
Stephen23 2021 年 4 月 13 日
Do not multiply by 8.
Dhinesh K
Dhinesh K 2021 年 4 月 13 日
Matlab compiler automatically taking bytes as 8. so howmany variables present in workspace its automatically multiplied with 8 bytes.
Dhinesh K
Dhinesh K 2021 年 4 月 13 日
If loaded variable datatype is uint16 means, it is 2 bytes know. but matlab takes as default 8 bytes.
According to doc of uint8, it only takes 1 byte. And on my R2020a, it indeed takes 1 byte.
clear
var1=uint8(1);
whos
Stephen23
Stephen23 2021 年 4 月 13 日
編集済み: Stephen23 2021 年 4 月 14 日
@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
Name Size Bytes Class Attributes x 1x1 8 double y 1x1 2 uint16
and whos correctly returns the number of bytes for each variable. It is not clear what you expect to happen.
Light = Simulink.Signal;
Light.CoderInfo.StorageClass = 'ImportedExtern';
Light.CoderInfo.Alias = '';
Light.CoderInfo.Alignment = -1;
Light.CoderInfo.CustomStorageClass = 'Default';
Light.Description = '';
Light.DataType = 'uint8';
Light.Min =0;
Light.Max =1;
Light.DocUnits = '';
Light.Dimensions = -1;
Light.DimensionsMode = 'Auto';
Light.Complexity = 'Auto';
Light.SampleTime = 0.01;
Light.SamplingMode = 'Auto';
Light.InitialValue = '';
I have created a variable for my simulation purpose, if i load this variable to workspace the no of bytes taking as 8bytes. so if i add one more variables like this bytes takes 8+8=16. so each variables takes as 8 bytes. thats is my problem now.
Dhinesh K
Dhinesh K 2021 年 4 月 13 日
@Stephen Cobeldick Is it clear now?
Dhinesh K
Dhinesh K 2021 年 4 月 15 日
@Stephen Cobeldick thank you for your reference.
I tried in 2018b matlab, but in this its taking as 8 bytes for each variables. so i got struct in this. .bytes showing 8bytes
Stephen23
Stephen23 2021 年 4 月 15 日
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
Dhinesh K
Dhinesh K 2021 年 4 月 15 日
編集済み: Stephen23 2021 年 4 月 15 日
@Stephen Cobeldick Please find my below code.
bdclose all; V_Bytes=0;
[file,path] = uigetfile('*.m','Select One or More Files','MultiSelect', 'on');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
end
[file1,path1] = uigetfile('*.slx','Select One or More Files','MultiSelect', 'on');
if isequal(file1,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path1,file1)]);
end
run(file);
load_system(file1);
strrep(file1,'.slx','');
In = find_system(bdroot,'SearchDepth','1','BlockType','Inport');
for idx=1: length(In)
V_Detials=whos(In{idx});
V_Bytes=V_Detials.bytes;
end
Stephen23
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
Dhinesh K 2021 年 4 月 15 日
Ok thank you @Stephen Cobeldick

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Operating on Diagonal Matrices についてさらに検索

タグ

質問済み:

2021 年 4 月 13 日

コメント済み:

2021 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by