How to get Data type of workspace loaded variables and how add that datatypes using script?
3 次查看(过去 30 天)
显示 更早的评论
Dhinesh K
2021-4-13
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.
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
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 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
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?
回答(1 个)
Xingwang Yong
2021-4-13
s=whos;
numBytes = sum([s.bytes]);
14 个评论
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.
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
2021-4-13
If loaded variable datatype is uint16 means, it is 2 bytes know. but matlab takes as default 8 bytes.
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
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.
Dhinesh K
2021-4-13
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.
Stephen23
2021-4-14
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
2021-4-15
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
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
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.
另请参阅
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)