x.abs()?

>> 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."

 Risposta accettata

Fangjun Jiang
Fangjun Jiang il 11 Nov 2011

1 voto

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.
  1. class(x) returns double
  2. methodsview(x) causes an error.

5 Commenti

Jonathan
Jonathan il 11 Nov 2011
I agree. The reason methodsview(x) causes an error is because isobject(x) returns false. From the help on isobject: "ISOBJECT(A) returns 1 if A is a MATLAB object and 0 otherwise." Juxtaposing this with a line from the Programming Fundamentals help section provides some clarity. "There are 15 fundamental classes in MATLAB." double is one of these fundamental MATLAB classes. However, it is not considered a MATLAB object.
That it is a class can be clearly seen from executing "class(x)", as above, or examining the metaclass for the double type: "mc = ?double". This reveals that there are native methods for the double class just as there are native methods for any MATLAB class. The particular methods for the double class can be inspected via the struct in mc or with the command "methods(x)", also "methods double". Also note that "hand = mc.Methods{1}.DefiningClass" returns a handle such that "isequal(hand, mc)" returns true.
Daniel Shub
Daniel Shub il 11 Nov 2011
@Jonathan, Why do you say that instances of the class double are not considered objects? In the "Subclassing MATLAB Built-In Classes" documentation it says "You can create an object of class double using an assignment statement, indexing expressions, or using converter functions." I read that as instances of class double are in fact objects and should obey all the rules of the MATLAB OO structure.
I look at the fact that isobject(double.empty) returns false as being an inconsistency in MATLAB arising from the fact that everything used to be a double.
Jonathan
Jonathan il 11 Nov 2011
@Daniel, This is a very good point. The reason I say that instances of the class double are not considered objects is because I use the result of isobject as part of my definition of object. In this sense, a 'MATLAB object' is anything that MATLAB treats as an object.
Shunchao Wu
Shunchao Wu il 19 Nov 2011
I'd like to consider double as one fundmental data type, not the so called "class", and x as a normal variable, not an object. With regard to class(x), methods(x), methodsview(x) and isobject(x), I regard them as some games.
Shunchao Wu
Shunchao Wu il 19 Nov 2011
Thank you all:)

Accedi per commentare.

Più risposte (2)

Daniel Shub
Daniel Shub il 11 Nov 2011

2 voti

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 Commento

Fangjun Jiang
Fangjun Jiang il 11 Nov 2011
+1. Interesting finding! I guess TMW has some inconsistency problem needs to be sorted out.

Accedi per commentare.

Drew Weymouth
Drew Weymouth il 11 Nov 2011

0 voti

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.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by