0

I have a class ( Matlab 2012a, Ubuntu 12.10)

classdef trajectory

   properties
       partName;
       coordinates;
   end

   methods
   end

end

How can I restrict the property partName to only be one of the elements of the set {'leftHand','rightHand'} ( both are strings) ?

Gnattuha
  • 211
  • 3
  • 11

1 Answers1

0

You can use the property set method eg.

classdef trajectory   
   properties
       partName;
       coordinates;    
   end

   methods
        function this=set.partName(this,myStr)
            mySet={'leftHand','rightHand'} ;
            if any(strcmp(mySet,myStr))
                this.partName=myStr;
            else
                error('Value not part of set');
            end
        end    
     end 
  end
Philliproso
  • 1,278
  • 1
  • 9
  • 16
  • wouldn't you need to make the property private to protect it? – Shai Mar 06 '13 at 07:28
  • @Shai there is no need to make set methods private for more info on when a set method is called, [MWhelp](http://www.mathworks.com/help/matlab/matlab_oop/property-access-methods.html) – Philliproso Mar 06 '13 at 09:08
  • @Philliproso You actually cannot make `set` methods private. They can only be defined within a `methods` block with no attributes. Although, as you say, there's no need to make the property private, you would probably want to give it a default value of either `leftHand` or `rightHand`. – Sam Roberts Mar 06 '13 at 15:00
  • Thank you guys! Link with enumeration helped! Solution by Philliproso is good as well. – Gnattuha Mar 06 '13 at 16:30