MATLAB objects and subsref

Oh MATLAB, why do you have to be so obtuse about what you are doing??? I understand that OO stuff is being shoved into a language that didn't originally support it, but WHY isn't it just documented better?

Problem: All of a sudden, my method calls from outside the class stopped working.

Background: All the documentation says that if you have a (dependent) property and there is public access to it, the methods get.propname(obj) and set.propname(obj, val) will be automatically called for you. This is true.

At least, it was until I decided I wanted my class to be able to return more than one value at a time. Once I defined at subsref() method, all external access died and now goes through it instead. EVERYTHING. So you must handle all your own public methods, etc, with useless wrapper logic. MATLAB doesn't do something intelligent like, "well, let me see… OK, there's a get.propname() so I'll call that." Nope. Instead, I need to do:

% (All within a "classdef")
function ar = subsref(obj, subs)
  switch subs(1).type
    case '.'
      switch subs(1).subs
        case 'single_val'
          ar = obj.single_val; % which DOES call the get.single_val() method!!!
      end % subs
    case '()'
      % actually do the stuff I wanted to do with ranges here
  end % type
end % subsref

This really confused me and I didn't find much on their website that was helpful about it either.

  • Posted: 15 years ago
  • Author: RevRagnarok
  • Categories: (none)


No comments.