- Undocumented Matlab - http://undocumentedmatlab.com -

Setting class property types – take 2

Posted By Yair Altman On April 27, 2016 | 11 Comments

Three years ago, almost to the day, I wrote about a very handy undocumented feature of Matlab classes that enables us to specify type restrictions for any Matlab class property [1]. We can specify property type (for example, char, double or any Matlab class) as well as dimensionality (scalar, vector, or matrix) and complexity indication (complex). Doing so has multiple benefits for code performance, robustness and maintainability. For example:

% Undocumented syntax - works well since at least R2010a (possibly earlier)
classdef Packet
        PayloadLength@uint16 scalar = uint16(0);  % initial value
        PacketData@uint8 vector

In the recent release of Matlab R2016a, a similar feature have finally become fully supported and documented [2]. The corresponding snippet above would look something like this:

% Documented syntax - only works in R2016a or newer
classdef Packet
        PacketType char
        HeaderLength uint16
        PayloadLength uint16 = uint16(0);  % initial value
        PacketData uint8

Unfortunately, I dislike the new documented functionality, so I didn’t feel like promoting it in this blog when it came out. But since a blog reader mentioned it [3] a few days ago, I wanted to come out publicly with my opinion and a detailed explanation.

If you look closely at the code snippets above, you will notice two important differences:

  1. The “@” symbol was replaced with a space
  2. The dimensionality and complexity cannot be specified

The new syntax has some drawbacks compared to the previous (undocumented) one:

  1. Backward compatibility – We can run the older (undocumented) syntax on any Matlab release since who-knows-when (at least as far back as R2010a [tested], and possibly older releases [untested]), including the very latest R2016a. On the other hand, the new (documented) syntax will only work on R2016a and will crash the program if you try to run it in older releases. This is not even something that you can catch with a try-catch block – the class will simply not load on any older Matlab release. If you need your code to run on older releases in addition to 16a, you have no choice other than to use the older syntax.
  2. Dimensionality – the new syntax, unlike the undocumented syntax, does not enable users to limit the data dimensionality (scalar/vector/array). This is a very important feature for program robustness and maintainability. Complexity is another type limitation that is missing, although it is less important than the dimensionality. And just in case you were wondering – the new syntax does not accept the additional scalar, vector, matrix and complex attributes like the older syntax; using them with the new syntax evokes an error.
  3. Cross compatibility – it is very confusing to users coming to Matlab from other programming languages, all of which (without any important exception) place the type name to the LEFT of the identifier name, not to its RIGHT. People coding in both Matlab and Java/C/C++ would easily get confused and frustrated.
  4. Consistency – despite what I hoped, the new syntax still does not apply to function input args: we cannot (AFAIK) limit the input/output args of methods/functions in the same way that we can limit properties. If there’s a way to do this, I’d be delighted to learn (this comment [4] may indicate that it is work in progress). It is true that this feature is not a drawback of the new syntax compared to the older one, since the old syntax didn’t have it either (AFAIK). But I would have expected a documented feature to be consistent across the Matlab language (or at least across the MCOS subset), and unfortunately the new feature fails this test.

In fact, aside from the fact that the new syntax is documented, I can see no advantages that it offers over the older syntax, only disadvantages. Or am I missing something? Please do tell if you see any important advantages that I’ve missed.
Luckily for us, the old syntax remains operational, side-by-side with the new one. This enables us to keep running our existing code without worrying [too much] that it might break in R2016a. Maybe the new syntax will grow on me (or improve) in upcoming years, but for the time being I see no benefit in switching away from the @ syntax.
For the past few years, I hoped that the property typing feature will become documented and that it will be a continuation of the undocumented syntax rather than what eventually aired. I’m afraid it’s too late to revert it now that it has… Realistically speaking, the best we can hope for now is for the older syntax to remain operational, and not be withdrawn in some future Matlab release. Making the undocumented syntax documented as-is would be great, but I’m afraid it is unrealistic given the new circumstances.
I’m sorry if I take the wind off MathWorks’ sails a bit here, but MathWorks knows that it can count on me to speak my mind without bullshit. Sometimes for the good, sometimes not. All in good spirit and the common interest of improving Matlab over time. No offence intended – it’s just my personal opinion after all.
In my opinion this is one of those rare cases where the developers obviously intended to make something better but eventually came out with something worse. They should have stuck to what was. After all, the first and foremost rule of engineering is, and always was:

Don’t fix it if it ain’t broke!

Categories: Medium risk of breaking in future versions, Undocumented feature

11 Comments (Open | Close)

11 Comments To "Setting class property types – take 2"

#1 Comment By Martin Afanasjew On April 28, 2016 @ 22:58

@Yair – Addressing your “In fact, aside from the fact that the new syntax is documented, I can see no advantages that it offers over the older syntax, only disadvantages. Or am I missing something? Please do tell if you see any important advantages that I’ve missed.”

Some may see it as an advantage, but I think the following is making things even worse. In addition to different syntax there is also different semantics of the old (undocumented) and new property type declarations. Assuming obj is an instance of a class with the following properties block

OldStyle@char = 'old'
NewStyle char = 'new'

there is this interesting behavior:

›› obj.OldStyle = 65;
While setting the 'OldStyle' property of 'Properties2016a': Value must be 'char'. 

›› obj.NewStyle = 65;
›› obj.NewStyle
ans =

This means the new syntax automatically converts to the declared type, if possible. (To be fair, this is documented.) The old syntax is more strict and throws an error on type mismatch, something I find much more desirable.

The following transcript reveals the difference in internal representation:

›› m = metaclass(obj);
›› m.PropertyList(1).Type.Name
ans =
›› m.PropertyList(2).Type.Name
ans =
char property

This also means that writing NewStyle@char property is equivalent to NewStyle char and will give the new behavior using the old syntax. All of this feels pretty convoluted and like you I fail to see the benefit of creating yet another syntax instead of documenting (and extending) the old syntax that has existed for a long time.

#2 Comment By YishaiE On April 29, 2016 @ 03:53

Isn’t inputParser a solution to the function input typecheck issue (kludgy and convoluted, but functional)?

#3 Comment By Yair Altman On April 30, 2016 @ 20:25

@YishaiE – inputParser is simply a function that checks the validity inputs. It is essentially no different than using other programmatic solutions such as assert or isa() etc. The potential benefits of constricting the input args at the function declaration level is that it would avoid the need to add these programmatic checks, and also the overheads associated with the function/method call in case of a type mismatch. Moreover, the code becomes cleaner, smaller, more robust and more maintainable. These are basically the same benefits of constricting property types.

Hag Same’ach!

#4 Comment By Ulrik On April 29, 2016 @ 20:57

Just a warning:
The @ for properties only works well for built-in objects in the older versions of Matlab.
If you use the @ for custom classes you can crash Matlab if the path to the classdef becomes unknown (e.g. you have not set it using addpath or the path is modified in a bad way).
Some of the built-in stuff in Matlab is written a bit “alternative” (e.g. uigetdir) and can trigger and a bug where the Matlab UI path goes out of sync with the actual path.
The bug seems to be (Matlab internal) event related.

Now with the new functionality I am looking forward to testing if this bug has been fixed or not. If not Mathworks can expect a bug-report.

#5 Comment By Egon On May 1, 2016 @ 15:28

Cross compatibility – it is very confusing to users coming to Matlab from other programming languages, all of which (without any important exception) place the type name to the LEFT of the identifier name, not to its RIGHT. People coding in both Matlab and Java/C/C++ would easily get confused and frustrated.
I disagree with this. There are quite a few languages that annotate the type to the right of variable names. Just have a look at Pascal (for an old language), and newer (and popular) languages such as Scala, Swift, Go, Rust, …: all of those define the type after the variable name.

Technically, this simplifies semantic analysis and is easier when type inference is needed.
From a human point of view, I also think it makes more sense. Specifically: the important part is the name of a variable (what does it mean?) rather than its type (implementation detail).
More info also at [11].

I hence really like that in MATLAB the type declaration is after the field name (no need to replicate the behavior of languages such as C that were explicitly written to be really concise and have quite some peculiarities as a result: e.g. defining both a variable and a pointer on the same line is possible but confusing).

On the other hand, I fully agree with your main point that MATLAB should have just documented the old syntax and not create new ones that are not backwards compatible. But if I’m completely honest, I would rather have seen the syntax as in Pascal, Swift, Scala, …:

name : type = value

where the :type part is optional from the start.
I particularly dislike how the old syntax uses @while those types don’t have an obvious link to function handles, and how this only works for properties, not for function/method arguments.

#6 Comment By Yair Altman On May 2, 2016 @ 01:32

@Egon – surely you are not equating the importance of Pascal (which I love, don’t get me wrong) et al, with that of C/C++/C#/Java. By whichever measure that you choose, these 4 latter languages far outweigh the others in importance.

Moreover, even the sample languages that you cite use the : operator to separate the identifier from its type, and in this respect they are much closer to Matlab’s old (undocumented) syntax that uses the @ operator.

Finally, the @ syntax is actually consistent with Matlab classes: after all, Matlab classes use @ to indicate [12] that are implemented as independent m-files, and also to [13] from a sub-class.

#7 Comment By Adam On May 3, 2016 @ 18:37

Thanks, this is certainly interesting. More so for me because I somehow missed your previous post from 3 years ago and knew nothing about the undocumented syntax (or the new documented one) previously! I did see some new appdesigner code recently though which included this new syntax and surprised me since I had never seen it before and thought it must somehow be special for appdesigner-produced code.

I have been using assignments to e.g. someClass.empty in a properties block to act mostly as documentation about what is expected and help with certain array-based initialisations and always use a validateattributes block in property set functions, but using this syntax (well, the undocumented older one at least) is potentially something I will look at for new classes.

#8 Comment By Simon On May 4, 2016 @ 18:58

Matlab typing in general isn’t great. Try this;

inceptDate datetime = datetime([],[],[]);  % The period start date as datetime

Then set it like to the string –


Matlab accepts this in both old and new ‘type checking’ formats. If you retrieve the inceptdate variable, it’s a character array, not a datetime.
Functionally then, you *still* need to roll your own type checking, whether you use the old or new style.

For small projects this is okay. For larger living projects, not having type safety is terrible…

#9 Comment By Chris Hoogeboom On June 17, 2016 @ 15:30

Hi Yair,

I agree with you on all of your points, especially with regards to the backwards compatibility issue. One nice thing that I noticed while reading through the documentation is that you can now specify properties to be a part of an enumeration class, which then allows you to set those properties as strings. For instance, suppose I have an enumeration

classdef WeekDay

Now I can define a class as such

classdef DayExample
        day WeekDay = WeekDay.Monday

and now I can do

obj = DayExample();
obj.day = WeekDay.Monday;
obj.day = 'Tuesday';

where in previous versions of MATLAB, I would have to implement it as

classdef DayExampleLegacy01
        day@WeekDay = WeekDay.Monday

        function this = set.day(this, d)
            if ischar(d) && isvector(d)
                d = WeekDay.(d);
            this.day = d;


classdef DayExampleLegacy02
        day@char = 'Monday'

        function this = set.day(this, d)
            WEEK_DAYS = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'};
            assert(ismember(d, WEEK_DAYS), ...
                'DayExampleLegacy02:IllegalDay', ...
                '"%" is not a valid week day.', d);
            this.day = d;

#10 Comment By Yair Altman On June 22, 2016 @ 10:47

@Chris – nice to see you here!

Your report chimes with [14]. The new syntax’s documented automatic type-casting can be replicated in the old (undocumented @) syntax using the property attribute:

classdef DayExample
        day@WeekDay property = WeekDay.Monday

which allows setting the value as either an enumerated value or its char representation:

>> d = DayExample
d = 
  DayExample with properties:

    day: Monday

>> d.day = WeekDay.Tuesday
d = 
  DayExample with properties:

    day: Tuesday

>> d.day = 'Thursday'
d = 
  DayExample with properties:

    day: Thursday

#11 Comment By anonymous On March 16, 2017 @ 11:46

Actually, Matlab has relased a new syntax to define property restrictions (Size, Class, Validation Functions):


#12 Comment By Kyle On October 23, 2019 @ 21:25

I have serious issues with the new syntax actually. If you want to have abstract classes, the old method would work. The new method does not. For example the below 3 classes work with the old way, but the new way does not. Also sorry for the formatting. I couldn’t seem to indent and add spaces properly

classdef Printer
       %graphicToPrint Graphic  % doesn't work
        function print(this)
            % do some printing acting on graphicToPrint

classdef Graphic
    methods (Abstract)

classdef ConcreteGraphic < Graphic
        function print(this)
            disp('print on ConcreteGraphic happened')

>> p = Printer;
>> p.graphicToPrint = ConcreteGraphic;
>> p.print
print on ConcreteGraphic happened

#13 Comment By Martin Lechner On October 31, 2019 @ 13:59

Hi Kyle,

I normally initialize the property with

    graphicToPrint Graphic = ConcreteGraphic.empty

and have no problems with the new syntax.

Article printed from Undocumented Matlab: http://undocumentedmatlab.com

URL to article: http://undocumentedmatlab.com/articles/setting-class-property-types-2

URLs in this post:

[1] type restrictions for any Matlab class property: http://undocumentedmatlab.com/blog/setting-class-property-types

[2] documented: http://www.mathworks.com/help/matlab/matlab_oop/restrict-class-of-properties.html

[3] mentioned it: http://undocumentedmatlab.com/blog/setting-class-property-types#comment-374984

[4] this comment: http://undocumentedmatlab.com/blog/setting-class-property-types#comment-343150

[5] Setting class property types : http://undocumentedmatlab.com/articles/setting-class-property-types

[6] Handle object as default class property value : http://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value

[7] Getting default HG property values : http://undocumentedmatlab.com/articles/getting-default-hg-property-values

[8] Types of undocumented Matlab aspects : http://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects

[9] Undocumented plot marker types : http://undocumentedmatlab.com/articles/undocumented-plot-marker-types

[10] Class object tab completion & improper field names : http://undocumentedmatlab.com/articles/class-object-tab-completion-and-improper-field-names

[11] : http://stackoverflow.com/questions/1712274/why-do-a-lot-of-programming-languages-put-the-type-after-the-variable-name

[12] : http://www.mathworks.com/help/matlab/matlab_oop/organizing-classes-in-folders.html

[13] : http://www.mathworks.com/help/matlab/matlab_oop/calling-superclass-methods-on-subclass-objects.html

[14] : http://undocumentedmatlab.com/blog/setting-class-property-types-2#comment-375230

[15] : https://de.mathworks.com/help/matlab/matlab_oop/validate-property-values.html

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.