Can you use the whos command to identify classes

3 次查看(过去 30 天)
As the title says, I'm wondering if the whos command (or an equivilent) could be used to get the range and storage requirements on a class in Matlab. For example, the 'double' class ranges from -10^308 to 10^308 amd needs 8 bytes of storage. How would I get these specs for, say, int8? Lookng on the wiki https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html does not give this information, so can it be obtained with a script? Thanks

采纳的回答

Walter Roberson
Walter Roberson 2022-10-5
numeric_classes = {'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'single', 'double'};
num_classes = length(numeric_classes);
T = table('size', [num_classes, 4], 'VariableNames', {'Class', 'bytes', 'min', 'max'}, ...
'VariableTypes', ["categorical", "double", "categorical", "categorical"]);
numeric_classes_cats = categorical(numeric_classes);
for K = 1 : num_classes
class_name = numeric_classes{K};
class_cat = numeric_classes_cats(K);
sample_variable = zeros(1, 1, class_name);
class_info = whos('sample_variable');
class_bytes = class_info.bytes;
class_bytes_str = string(class_bytes);
if isfloat(sample_variable)
class_max = realmax(class_name);
class_min = -class_max;
class_min_str = categorical(compose("%.16g", class_min));
class_max_str = categorical(compose("%.16g", class_max));
else
class_min = intmin(class_name);
class_max = intmax(class_name);
class_min_str = categorical(class_min);
class_max_str = categorical(class_max);
end
T(K, :) = {class_name, class_bytes_str, class_min_str, class_max_str};
end
T
T = 10×4 table
Class bytes min max ______ _____ _______________________ ______________________ int8 1 -128 127 uint8 1 0 255 int16 2 -32768 32767 uint16 2 0 65535 int32 4 -2147483648 2147483647 uint32 4 0 4294967295 int64 8 -9223372036854775808 9223372036854775807 uint64 8 0 18446744073709551615 single 4 -3.402823466385289e+38 3.402823466385289e+38 double 8 -1.797693134862316e+308 1.797693134862316e+308

更多回答(2 个)

Image Analyst
Image Analyst 2022-10-4
Try this:
a=uint8([1,2,3]);
dbl = 123.456;
str = 'abc';
s = whos
s = 4×1 struct array with fields:
name size bytes class global sparse complex nesting persistent
t = struct2table(s)
t = 4×9 table
name size bytes class global sparse complex nesting persistent __________ _______ _____ __________ ______ ______ _______ __________ __________ {'a' } 1 3 3 {'uint8' } false false false 1×1 struct false {'cmdout'} 1 33 66 {'char' } false false false 1×1 struct false {'dbl' } 1 1 8 {'double'} false false false 1×1 struct false {'str' } 1 3 6 {'char' } false false false 1×1 struct false
  3 个评论
Image Analyst
Image Analyst 2022-10-5
Did you mean a class definition or an instance of a class. Of course a class definition is just a definition of a framework that could contain variables (properties). So you can't get a size of that. However an instance of a class is a variable and you can get the size it uses up with the whos command like I showed.
Image Analyst
Image Analyst 2022-10-5
OK I see what you want now, now that I see Walter's answer which you accepted. Of course Walter has the advantage of having the Mind Reading Toolbox, which I don't have yet.
You wanted the range of built-in classes like uint8, double, etc. I thought you wanted classes like classes you made yourself with the classdef command (like my attached Excel utility class), or instances of those classes (actual variables you instantiated as being an instance of the class) or instances of built-in class variables.

请先登录,再进行评论。


Steven Lord
Steven Lord 2022-10-5
For the integer types see this documentation page.

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by