現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to get Data type of workspace loaded variables and how add that datatypes using script?
4 ビュー (過去 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.
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT-Files についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)