« Earlier2 items total Later »

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.

Creating reference objects

You will notice some strange notation in some of the Matlab toolboxes, schema.this, schema.that. It pretty obvious this is some rudimentry class and object system for creating reference objects. However it is not documented or supported by The Mathworks so it is probably not a good idea going overboard in using any of it in your projects.

However you can use a small set of these commands to create reference objects. Define the following function.

function h = reference

   % Declare the new class
   p = findpackage('xt');
   if isempty(p)
      p = schema.package('xt');
   end
   c = p.findclass('reference');
   if isempty(c)
      c = schema.class(p, 'reference');
   end
   h = xt.reference;

   %Create a single property 'data'
   prop = schema.prop(h, 'data', 'mxArray');
end


You can then store information in the data field. Unlike standard matlab structure this is a reference structure. So you can do the following.

>> h.data = 10;
>> g = h;
>> h.data = 20;
>> g.data == h.data

ans =

     1


The data field is indexable using any matlab array operators. This means you can pass a reference structure to a function and have it do an in-place operation instead of an expensive copy.

« Earlier2 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.