Methods of Matlab's Built-in Objects - matlab

What are the differences between these two calling sequences get(a) and a.get()?
load gong
a = audioplayer(y, Fs);
a.get
get(a)

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.

Related

MATLAB set class `size` (overload operation upon class)

classdef Dog
end
d=Dog(); can size(d) be controlled? Is there some property to set or method to overload?
Ultimately I have like d.data = [1, 2, 3] and want length(d) == 3. I'm aware I can make d.length(). As an aside, is there a list of MATLAB "magic methods", i.e. functions that control interaction with classes, like subsref?
In MATLAB, don't think of class method as similar to class methods in other languages. Really, what they are is overloaded versions of a function.
size(d) is the same as d.size() (which is the same as d.size, parentheses are not needed to call a function), if d is an object of a custom class and size is overloaded for that class.
So, you can define function size in the methods section of your classdef, to overload size for your class. You can also create a size.m file within a #Dog/ directory to accomplish the same thing.
For example, if you create a file #char/size.m with a function definition inside, you will have overloaded size for char arrays.
The above is true for any function. Some functions, when overloaded, can cause headaches. For example be careful when overloading numel, as it could cause indexed assignment expressions to fail. size is used by the whos command to display variable information, as well as by the similar functionality in the GUI, so it is important to have it behave in the expected way.
The object behavior that you might want to change that is not obviously a function, relates to operators. Each operator is also defined by a function (including end in indexing!). See the docs for a full list.

MATLAB, Phased Array Toolbox and `pattern` function

I'm trying to understand an example for MATLAB Phased Array Toolbox.
There is called function pattern:
pattern(ha,fc,-180:180,0,'Type','powerdb',...
'CoordinateSystem','rectangular','PropagationSpeed',c)
Cannot you tell me, where is the function described and what does it do?
pattern is a method on antenna array objects. ha is an instance of the class phased.ReplicatedSubarray. When object is the first argument to a call, MATLAB first checks whether this is a method on that object and calls that. This syntax is same as calling ha.pattern(...). If you try methods(ha) it should list pattern as one of the methods. You can find documentation for pattern method of this object at http://www.mathworks.com/help/phased/ref/phased.replicatedsubarray.pattern.html.

about matlab's Adaptfilt.<algorithm> function

Matlab's DSP toolbox has a function called adaptfilt., where calling adaptfilt is not enough, but you must add the .< algorithm> where the algorithm can be one of the many things, which we can view using help adaptfilt. What kind of Matlab structure has been used here (or what is the . operator and how can I make my own function that has to be called using a dot).
Also, the result of doing, say, adaptfilt.fdaf, gives a result that seems like a structure. How can I view all the elements of this structure (that is, if there are any more members besides the values that are returned on the screen by the function itself)?
adaptfilt is a class definition, of which fdaf is a member. Then, you use the dot operator to access the static member of the class. See Static Methods in the MATLAB documentation. In summary, to define a similar function yourself use
classdef MyClass
...
methods(Static)
function y = yourFunc(x)
...
end
end
end
The result you're getting from adaptfilt.fdaf is in fact an object. The adaptfilt.fdaf documentation page outlines the members of the object.

Why making a difference between methods and functions in Scala?

I have been reading about methods and functions in Scala. Jim's post and Daniel's complement to it do a good job of explaining what the differences between these are. Here is what I took with me:
functions are objects, methods are not;
as a consequence functions can be passed as argument, but methods can not;
methods can be type-parametrised, functions can not;
methods are faster.
I also understand the difference between def, val and var.
Now I have actually two questions:
Why can't we parametrise the apply method of a function to parametrise the function? And
Why can't the method be called by the function object to run faster? Or the caller of the function be made calling the original method directly?
Looking forward to your answers and many thanks in advance!
1 - Parameterizing functions.
It is theoretically possible for a compiler to parameterize the type of a function; one could add that as a feature. It isn't entirely trivial, though, because functions are contravariant in their argument and covariant in their return value:
trait Function1[+T,-R] { ... }
which means that another function that can take more arguments counts as a subclass (since it can process anything that the superclass can process), and if it produces a smaller set of results, that's okay (since it will also obey the superclass construct that way). But how do you encode
def fn[A](a: A) = a
in that framework? The whole point is that the return type is equal to the type passed in, whatever that type has to be. You'd need
Function1[ ThisCanBeAnything, ThisHasToMatch ]
as your function type. "This can be anything" is well-represented by Any if you want a single type, but then you could return anything as the original type is lost. This isn't to say that there is no way to implement it, but it doesn't fit nicely into the existing framework.
2 - Speed of functions.
This is really simple: a function is the apply method on another object. You have to have that object in order to call its method. This will always be slower (or at least no faster) than calling your own method, since you already have yourself.
As a practical matter, JVMs can do a very good job inlining functions these days; there is often no difference in performance as long as you're mostly using your method or function, not creating the function object over and over. If you're deeply nesting very short loops, you may find yourself creating way too many functions; moving them out into vals outside of the nested loops may save time. But don't bother until you've benchmarked and know that there's a bottleneck there; typically the JVM does the right thing.
Think about the type signature of a function. It explicitly says what types it takes. So then type-parameterizing apply() would be inconsistent.
A function is an object, which must be created, initialized, and then garbage-collected. When apply() is called, it has to grab the function object in addition to the parent.

The use of "this" in every MATLAB class

Why is that in every class in MATLAB I must use "this"? I think that in C++ I don't need to use "this", only if I want to. Is this also the case in MATLAB?
In short, you must use some kind of explicit reference.
First of all, unlike in C++/C#/Java where it is named this, you can use any name you want.
The reason that you must use explicit calls is Matlab designers decision.
The idea was to support Matlab vector operations on objects, as if they are
structs. The following is a fragment from the link above:
While languages with an implicit object parameter provide a "this" keyword
to access the implicit object, they usually do not require you to access a
property through "this". If MATLAB had implicit properties, the logical
extension to array-based objects would be to index into nothing:
S = S + (k).Value;
Edit:
Following the good comment of #AndrewJanke, I would like to add that MATLAB could have had this as implicit reference, and only force to use it in indexing of array-based objects. Nevertheless, this approach was not chosen by MATLAB designers.