You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to get Data type of workspace loaded variables and how add that datatypes using script?
5 views (last 30 days)
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.
Dhinesh K
on 16 Apr 2021
Edited: Dhinesh K
on 16 Apr 2021
@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
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 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
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?
Answers (1)
Xingwang Yong
on 13 Apr 2021
s=whos;
numBytes = sum([s.bytes]);
14 Comments
Dhinesh K
on 13 Apr 2021
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
on 13 Apr 2021
Matlab compiler automatically taking bytes as 8. so howmany variables present in workspace its automatically multiplied with 8 bytes.
Dhinesh K
on 13 Apr 2021
If loaded variable datatype is uint16 means, it is 2 bytes know. but matlab takes as default 8 bytes.
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
Stephen23
on 13 Apr 2021
Edited: Stephen23
on 14 Apr 2021
@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
on 13 Apr 2021
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
on 14 Apr 2021
Dhinesh K
on 15 Apr 2021
@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
on 15 Apr 2021
@Dhinesh K: please upload some sample data, and show the exact code that you are using.
Dhinesh K
on 15 Apr 2021
Edited: Stephen23
on 15 Apr 2021
@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
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.
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)