- class(x) returns double
- methodsview(x) causes an error.
x.abs()?
3 ビュー (過去 30 日間)
古いコメントを表示
>> x=-1;
>> x.abs()
??? Attempt to reference field of non-structure array.
Why?
Notice that Matlab Help says "All MATLAB data types are implemented as object-oriented classes."
0 件のコメント
採用された回答
Fangjun Jiang
2011 年 11 月 11 日
That is true, but it doesn't imply that x you declared will have abs as its method.
I think that claim you quoted means that MATLAB's data types are internally implemented as object-oriented classes. The double data type doesn't provide any method.
5 件のコメント
その他の回答 (2 件)
Daniel Shub
2011 年 11 月 11 日
I disagrees with both answers. Starting with
x = -1;
You can see that abs is a method with
methods(x)
metaclass(x)
methodsview(class(x))
Interesting, as Fangjun mentioned,
methodsview(x)
gives an error. Exploring the double class in general
?double
methodsview double
both list abs as a method.
Finally, creating a simply dummy class
classdef myClass < double
methods
function newClass = myClass(input)
newClass = newClass@double(input);
end
end
end
makes it so that
y = myClass(-1);
y.abs()
works.
1 件のコメント
Fangjun Jiang
2011 年 11 月 11 日
+1. Interesting finding! I guess TMW has some inconsistency problem needs to be sorted out.
Drew Weymouth
2011 年 11 月 11 日
Although your variable x is technically a MATLAB "class" ( double ), it does not have any methods associated with it. While MATLAB does have some built-in classes with methods (such as Audioplayer for example), most data types are basically primitive values. To get the absolute value of any scalar value, use y = abs(x). If x is a matrix, then this will take the absolute value of each component. If x is complex, then it will give the magnitude.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Construct and Work with Object Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!