Methods of Matlab's Built-in Objects

46 Views Asked by At

What are the differences between these two calling sequences get(a) and a.get()?

load gong
a = audioplayer(y, Fs);
a.get
get(a)
1

There are 1 best solutions below

0
Cris Luengo On

There is no difference by default. However, the behavior can be modified.

By default, for an object of a custom class, obj.method(arg1,arg2,...) is syntactic sugar for method(obj,arg1,arg2,...). This means that when you write the former, MATLAB pretends you wrote the latter and proceeds accordingly.

However, it is possible to overload the method subsref for the class, in which case this function will be called for the syntax obj.method(arg1,arg2,...). That is, MATLAB interprets it as an indexing operation (.method) followed by another indexing operation ((arg1,arg2,...)). The subsref method is called to evaluate these indexing operations. It is possible to implement it such that the appropriate method is called in this case, but custom indexing code is executed for other indexing operations such as obj(x) or obj{x}. See for example here.