Matlab 2008a
I don't do much Matlab these days but it is nice to see that release 2008a is out with the much waited for object oriented additions.It is definitely something that will make programming Matlab much easier for larger applications and do away with many of the hacks which I have illustrated myself on this website. However I am not sure I like the verbosity of the OO features.
For example there are provisions for
- attributes
- events
- methods
- listeners
whereas in Ruby you can make do with just instance variables and methods, the rest being sorted out with meta programming tricks. Mentioning metaprogramming I don't see any facility for this within the documentation though it is probably there under the hood.
Also not sure why you are restricted to creating classes in files. It would be neat and generally usefully to be able to create classes at the command line and/or multiple classes per file as is possible in Ruby/Python.
I also noted a boo boo in the documentation. Some doc writer claims that Matlab is weakly typed, when in fact they mean dynamically typed as opposed the to statically typed.
Matalb is strongly and dynamically typed whereas C/C++ is weakly and statically typed.
A simple example from the Mathworks home page
classdef BankAccount < handle properties (Hidden) AccountStatus = 'open'; end % The following properties can be set only by class methods properties (SetAccess = private) AccountNumber AccountBalance = 0; end % Define an event called InsufficientFunds events InsufficientFunds end methods function BA = BankAccount(AccountNumber,InitialBalance) BA.AccountNumber = AccountNumber; BA.AccountBalance = InitialBalance; % Calling a static method requires the class name % addAccount registers the InsufficientFunds listener on this instance AccountManager.addAccount(BA); end function deposit(BA,amt) BA.AccountBalance = BA.AccountBalance + amt; if BA.AccountBalance > 0 BA.AccountStatus = 'open'; end end function withdraw(BA,amt) if (strcmp(BA.AccountStatus,'closed')&& BA.AccountBalance < 0) disp(['Account ',num2str(BA.AccountNumber),' has been closed.']) return end newbal = obj.AccountBalance - amt; BA.AccountBalance = newbal; % If a withdrawal results in a negative balance, % trigger the InsufficientFunds event using notify if newbal < 0 notify(BA,'InsufficientFunds') end end % withdraw end % methods end % classdef
See Mathworks for more information.