casparjespersen
casparjespersen

Reputation: 3860

Defining custom method and property blocks on superclass in Matlab

In Matlab, is it possible to define custom method and property blocks for a classdef?

I.e.

classdef Foo < MyCustomSuperclass

   properties (SomeBlockA)
      Var1 = 2
      Var2 = 5
   end

   properties (SomeBlockB)
      Var3 = 2
      Var4 = 5
   end

end

My guess is that it would have to be defined on MyCustomSuperclass.

Upvotes: 2

Views: 88

Answers (1)

Sam Roberts
Sam Roberts

Reputation: 24127

No, you can't do that. Some built-in MATLAB classes (e.g. those inheriting from matlab.unittest.TestCase) have properties and methods with custom attributes (e.g. TestParameter etc), but MathWorks haven't yet given you the ability to create your own custom property or method attributes.

However, depending on why you want to do this, you may be able to abuse a piece of undocumented functionality to achieve what you want.

All class properties and methods (and events as well) have a pair of undocumented attributes Description and DetailedDescription, which must have a string as their value. So, for example, you can have:

classdef myclass
    properties (Description='SomeBlockA')
        var1=1;
    end
    properties (Description='SomeBlockB')
        var2=2;
    end
end

The class will function correctly at this point, but will give a red underline in the editor, indicating "Unknown attribute name 'Description'". This has no functional effect, but is annoying; you can suppress it by including the pragma %#ok<*ATUNK> (attribute unknown) in your code like this:

classdef myclass
%#ok<*ATUNK>
    properties (Description='SomeBlockA')
        var1=1;
    end
    properties (Description='SomeBlockB')
        var2=2;
    end
end

If you need to, you can query the Description attribute of a property using metaclasses:

>> a = ?myclass;
>> a.PropertyList(1).Description
ans =
SomeBlockA
>> a.PropertyList(2).Description
ans =
SomeBlockB

Hope that helps!

Upvotes: 3

Related Questions