Friday, January 27, 2017

Structures

MATLAB has structure data types.[15] Since all variables in MATLAB are arrays, a more adequate name is "structure array", where each element of the array has the same field names. In addition, MATLAB supports dynamic field names[16] (field look-ups by name, field manipulations, etc.). Unfortunately, MATLAB JIT does not support MATLAB structures, therefore just a simple bundling of various variables into a structure will come at a cost.[17]

Functions

When creating a MATLAB function, the name of the file should match the name of the first function in the file. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores. Functions are also often case sensitive.

Function handles

MATLAB supports elements of lambda calculus by introducing function handles,[18] or function references, which are implemented either in .m files or anonymous[19]/nested functions.[20]

Classes and object-oriented programming

MATLAB supports object-oriented programming including classes, inheritance, virtual dispatch, packages, pass-by-value semantics, and pass-by-reference semantics.[21] However, the syntax and calling conventions are significantly different from other languages. MATLAB has value classes and reference classes, depending on whether the class has handle as a super-class (for reference classes) or not (for value classes).[22]
Method call behavior is different between value and reference classes. For example, a call to a method
object.method();
can alter any member of object only if object is an instance of a reference class.
An example of a simple class is provided below.
classdef hello
    methods
        function greet(this)
            disp('Hello!')
        end
    end
end
When put into a file named hello.m, this can be executed with the following commands:
>> x = hello;
>> x.greet();
Hello!

No comments:

Post a Comment