<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Object oriented &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/object-oriented/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Fri, 30 Sep 2022 14:59:20 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Setting class property types &#8211; take 2</title>
		<link>https://undocumentedmatlab.com/articles/setting-class-property-types-2?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-class-property-types-2</link>
					<comments>https://undocumentedmatlab.com/articles/setting-class-property-types-2#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 27 Apr 2016 20:20:22 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[MCOS]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6390</guid>

					<description><![CDATA[<p>R2016a saw the addition of class property types. However, a better alternative has existed for many years. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-class-property-types-2">Setting class property types &#8211; take 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Three years ago, almost to the day, I wrote about a very handy undocumented feature of Matlab classes that enables us to specify <a href="/articles/setting-class-property-types" target="_blank">type restrictions for any Matlab class property</a>. We can specify property type (for example, <code>char</code>, <code>double</code> or any Matlab class) as well as dimensionality (<code>scalar</code>, <code>vector</code>, or <code>matrix</code>) and complexity indication (<code>complex</code>). Doing so has multiple benefits for code performance, robustness and maintainability. For example:</p>
<pre lang="matlab">
% Undocumented syntax - works well since at least R2010a (possibly earlier)
classdef Packet
    properties
        PacketType@char
        HeaderLength@uint16
        PayloadLength@uint16 scalar = uint16(0);  % initial value
        PacketData@uint8 vector
    end
end
</pre>
<p>In the recent release of Matlab R2016a, a similar feature have finally become fully supported and <a href="http://www.mathworks.com/help/matlab/matlab_oop/restrict-class-of-properties.html" rel="nofollow" target="_blank">documented</a>. The corresponding snippet above would look something like this:</p>
<pre lang="matlab">
% Documented syntax - only works in R2016a or newer
classdef Packet
    properties
        PacketType char
        HeaderLength uint16
        PayloadLength uint16 = uint16(0);  % initial value
        PacketData uint8
    end
end
</pre>
<p>Unfortunately, I dislike the new documented functionality, so I didn&#8217;t feel like promoting it in this blog when it came out. But since a blog reader <a href="/articles/setting-class-property-types#comment-374984" target="_blank">mentioned it</a> a few days ago, I wanted to come out publicly with my opinion and a detailed explanation.<br />
<span id="more-6390"></span><br />
If you look closely at the code snippets above, you will notice two important differences:</p>
<ol>
<li>The &#8220;@&#8221; symbol was replaced with a space</li>
<li>The dimensionality and complexity cannot be specified</li>
</ol>
<p>The new syntax has some drawbacks compared to the previous (undocumented) one:</p>
<ol>
<li><b>Backward compatibility</b> &#8211; 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 &#8211; 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.</li>
<li><b>Dimensionality</b> &#8211; 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 &#8211; the new syntax does <i>not</i> accept the additional <code>scalar</code>, <code>vector</code>, <code>matrix</code> and <code>complex</code> attributes like the older syntax; using them with the new syntax evokes an error.</li>
<li><b>Cross compatibility</b> &#8211; 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.</li>
<li><b>Consistency</b> &#8211; 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&#8217;s a way to do this, I&#8217;d be delighted to learn (<a href="/articles/setting-class-property-types#comment-343150" target="_blank">this comment</a> 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&#8217;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.</li>
</ol>
<p>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&#8217;ve missed.<br />
Luckily for us, <b>the old syntax remains operational, side-by-side with the new one</b>. 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.<br />
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&#8217;m afraid it&#8217;s too late to revert it now that it has&#8230; 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&#8217;m afraid it is unrealistic given the new circumstances.<br />
I&#8217;m sorry if I take the wind off MathWorks&#8217; 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 &#8211; it&#8217;s just my personal opinion after all.<br />
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:<br />
<center><i><b><font size="+3" color="red">Don&#8217;t fix it if it ain&#8217;t broke!</font></b></i></center></p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-class-property-types-2">Setting class property types &#8211; take 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types" rel="bookmark" title="Setting class property types">Setting class property types </a> <small>Matlab's class properties have a simple and effective mechanism for setting their type....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/setting-class-property-types-2/feed</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
		<item>
		<title>Setting class property types</title>
		<link>https://undocumentedmatlab.com/articles/setting-class-property-types?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-class-property-types</link>
					<comments>https://undocumentedmatlab.com/articles/setting-class-property-types#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 18 Apr 2013 14:12:06 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[MCOS]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<category><![CDATA[schema.prop]]></category>
		<category><![CDATA[UDD]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3725</guid>

					<description><![CDATA[<p>Matlab's class properties have a simple and effective mechanism for setting their type.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-class-property-types">Setting class property types</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types-2" rel="bookmark" title="Setting class property types &#8211; take 2">Setting class property types &#8211; take 2 </a> <small>R2016a saw the addition of class property types. However, a better alternative has existed for many years. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>When I wrote about the <a target="_blank" href="/articles/undocumented-classdef-attributes/">undocumented aspects of classdef properties</a> half a year ago, I did not expect to learn of another major undocumented aspect in Matlab&#8217;s class-object system. last month I discovered the additional undocumented classdef Description and DetailedDescription attributes, and updated that article accordingly. But a few days ago I was pleasantly surprised to read Amro&#8217;s <a target="_blank" href="/articles/undocumented-classdef-attributes/#comment-189056">comment</a> about an entirely new and undocumented aspect of Matlab MCOS classes.</p>
<p><a target="_blank" rel="nofollow" href="http://stackoverflow.com/users/97160/amro">Amro</a> is a top contributor on StackOverflow, where he frequently answers questions before I even get any subscription notification about them&#8230; His answers are generally characterized by a deep technical understanding of Matlab, and I&#8217;ve learned quite a lot from him in the past few years. This time was no different.</p>
<p>In a nutshell, Amro found an undocumented way to specify a class object property&#8217;s type, in such a way that would prevent accidental setting to an incompatible value. For example, if we have a class with properties <b>Width</b> and <b>Height</b>, we probably want to restrict their possible values to numbers, to prevent setting a string or struct value.</p>
<p>In UDD classes, we can do this easily by setting the property&#8217;s <b>DataType</b> meta-property. An easy way to do this is by setting the second argument of the <a target="_blank" href="/articles/creating-a-simple-udd-class/"><i><b>schema.prop</b></i> function</a>. A detailed explanation was provided <a target="_blank" href="/articles/udd-properties/">here</a>.</p>
<p>We can still do this today, since UDD classes are still supported, side-by-side with the newer MCOS classes. Unfortunately, MCOS does not provide a <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/matlab_oop/defining-properties.html">documented</a> way of specifying the property type as in UDD.</p>
<p>One simple way to prevent unintentional MCOS property updates is to <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/matlab_oop/implementing-a-set-get-interface-for-properties.html">override the property&#8217;s <i><b>set</b></i> method</a>. In fact, this was the <a target="_blank" rel="nofollow" href="http://stackoverflow.com/a/7192964/233829">solution</a> of Jonas, another StackOverflow heavyweight:</p>
<pre lang='matlab'>
classdef myClass
   properties
      myProperty = uint16(23); %# specify default value using correct type
   end
   methods
      function obj = set.myProperty(obj,val)
         if ~isa(val,'uint16')
            error('only uint16 values allowed')
         end
         %# assign the value
         obj.myProperty = val;
      end
   end
end
</pre>
<p>But it turns out that there&#8217;s a much cleaner and simpler solution, <a target="_blank" rel="nofollow" href="http://stackoverflow.com/a/15992558/233829">provided</a> by Amro:</p>
<pre lang='matlab'>
classdef Packet
    properties
        HeaderLength@uint16
        PayloadLength@uint16 = uint16(0);
        PacketType@char
    end
end
</pre>
<p><span id="more-3725"></span><br />
As Amro notes, if you try to set a property with the wrong type, you get an error:</p>
<div class="wp_syntax">
<div class="code">
<pre class="matlab" style="font-family:monospace;">&gt;&gt; p = Packet;
&gt;&gt; p.<span style="">PacketType</span> = <span style="color:#A020F0;">'tcp'</span>;  <span style="color: #228B22;">% ok</span>
&gt;&gt; p.<span style="">HeaderLength</span> = <span style="color: #33f;">100</span>;  <span style="color: #228B22;">% not ok - should be a uint16</span>
<span style="color: #FF0000;">While setting the 'HeaderLength' property of Packet:
Value must be 'uint16'</span>.</pre>
</div>
</div>
<p>This syntax apparently supports all primitive types (<i><b>char, int32, double, struct, cell</b></i> etc.), in addition to any user-defined ones (just use any class name).<br />
Note that setting the type as above seems to override any <i><b>set</b></i> method that may have been specified for the property.</p>
<p>Amro came across this syntax in an internal class in R2013a (<i>toolboxdir(&#8216;matlab&#8217;)/graphics/+graphics/+internal/+figfile/@FigFile/FigFile.m</i>), but it also worked in R2012a, and probably older versions as well&#8230;</p>
<p>I admit there&#8217;s not much original work here by me &#8211; it&#8217;s basically all by Amro (and Donn Shull for the UDD part). But I thought it&#8217;s important enough to bring to the attention of the community.</p>
<p>I love to discover such undocumented treasures by digging in Matlab&#8217;s function. If you ever discover other such buried treasures, please do let me know by email or a comment.</p>
<hr />
<p><b><u>Addendum June 21, 2013</u></b>: As Sebastian Hölz mentioned in his <a href="/articles/setting-class-property-types/#comment-215669">comment</a> below, the general syntax appears to be:</p>
<pre lang='matlab'>
properties
   propName@className dimensionType = initialValue
end
</pre>
<p>where <code>className</code>, <code>dimensionType</code> and <code>initialValue</code> are optional elements:</p>
<ul>
<li><code>className</code> can be any Matlab class/type, such as <code>double</code>, <code>single</code>, or <code>UserClass</code></li>
<li><code>dimensionType</code> can be one of the following terms: <code>scalar</code>, <code>vector</code>, or <code>matrix</code></li>
<li>the property may also be initiated with an <code>initialValue</code> (otherwise it receives a default value, depending on the property class)</li>
</ul>
<hr />
<p><b><u>Addendum August 12, 2014</u></b>: In a related matter, we can limit the values that a property can accept using the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/simulink/slref/matlab.system.stringset-class.html">matlab.system.StringSet</a> class of the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/simulink/slref/matlab.system-class.html">matlab.System</a> package, as recently <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/answers/138149-autocomplete-of-properties-for-hgsetget-derived-class#answer_144336">discovered</a> by Oleg Komarov (<a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/simulink/ug/limit-property-values-to-a-finite-set-of-strings-1.html">additional details</a>; <a target="_blank" href="/articles/class-object-tab-completion-and-improper-field-names">additional aspects of class property tab-completion</a>):</p>
<pre lang='matlab'>
classdef foo < matlab.System
    properties
        Coordinates
    end
    properties(Hidden,Transient)
        CoordinatesSet = matlab.system.StringSet({'north','south','east','west'});
    end
end
</pre>
<p><center><img decoding="async" alt="restricting Matlab property values" src="https://www.mathworks.com/matlabcentral/answers/uploaded_files/15194/Capture.PNG" title="restricting Matlab property values" width="309" height="129"/></center></p>
<hr />
<p><b><u>Addendum April 8, 2015</u></b>: As reported by Martin A. <a target="_blank" href="/articles/plot-legend-title#comment-347508">here</a> and also <a target="_blank" href="/articles/setting-class-property-types#comment-343150">below</a>, R2015a has apparently added the new (and hidden) <em>Type</em> property to the <em>meta.property</em> class and the <em>InputTypes</em> and <em>OutputTypes</em> properties to the the <em>meta.method</em> class. This enables specifying and querying the types of properties as well as method inputs/outputs. For example:</p>
<pre lang="matlab">
mc = ?matlab.graphics.axis.Axes;
mc.PropertyList(1).Name % Camera
mc.PropertyList(1).Type % matlab.graphics.axis.camera.Camera
mc.MethodList(49).Name % addlistener
mc.MethodList(49).InputNames % [sources, propertyname, eventname, callback]
mc.MethodList(49).InputTypes % [handle, asciiString, char vector, function_handle scalar]
</pre>
<hr />
<p><b><u>Addendum November 11, 2015</u></b>: <a href="/articles/setting-class-property-types#comment-362420">Apparently</a>, when using the <code>@double</code> syntax, Matlab only allows the property to receive real values. In order to enable complex values, we can also add the undocumented "complex" attribute. Note that we have to enter the dimensionality (scalar/vector/matrix) first, since "complex" is not accepted as a valid dimensionality value. For example:</p>
<pre lang='matlab' highlight='4,11'>
% This is ok:
classdef foo
    properties
        bar@double scalar complex = 1 + 2i
    end
end
% This causes an error:
classdef foo
    properties
        bar@double complex = 1 + 2i
    end
end
</pre>
<hr />
<p><b><u>Addendum April 28, 2016</u></b>: Matlab R2016a now has a similar documented functionality. However, I feel that there are important drawbacks to the new functionality compared to the one presented here. Read the <a href="/articles/setting-class-property-types-2" target="_blank">full discussion about this</a>.<br />
The new (documented) syntax automatically type-casts the data to the specified data type, were possible, something that the undocumented (@) syntax presented above does not. In other words, the undocumented syntax is stricter and expects the data to be exactly the specified data type, without attempting any type-casting. As <a href="/articles/setting-class-property-types-2#comment-375230" target="_blank">reported by Martin Afanasjew</a>, we can add the <code>property</code> attribute to the data type to replicate the new syntax's automatic type-casting:</p>
<pre lang="matlab" highlight='4'>
classdef foo
    properties
        prop1@char = 'A'  % will only accept char inputs, not numeric values
        prop2@char property = 65  % will automatically type-cast: 65 will be converted to 'A'
    end
end
</pre>
<hr />
<p><b><u>Addendum September 30, 2022</u></b>: Matlab R2022a has made the legacy @ syntax unsupported, displaying an error message about it in the Matlab Editor, and generating a run-time error if you try to use a class that uses this syntax.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/setting-class-property-types">Setting class property types</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/setting-class-property-types-2" rel="bookmark" title="Setting class property types &#8211; take 2">Setting class property types &#8211; take 2 </a> <small>R2016a saw the addition of class property types. However, a better alternative has existed for many years. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/handle-object-as-default-class-property-value" rel="bookmark" title="Handle object as default class property value">Handle object as default class property value </a> <small>MCOS property initialization has a documented but unexpected behavior that could cause many bugs in user code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/getting-default-hg-property-values" rel="bookmark" title="Getting default HG property values">Getting default HG property values </a> <small>Matlab has documented how to modify default property values, but not how to get the full list of current defaults. This article explains how to do this. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/types-of-undocumented-matlab-aspects" rel="bookmark" title="Types of undocumented Matlab aspects">Types of undocumented Matlab aspects </a> <small>This article lists the different types of undocumented/unsupported/hidden aspects in Matlab...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/setting-class-property-types/feed</wfw:commentRss>
			<slash:comments>41</slash:comments>
		
		
			</item>
		<item>
		<title>Undocumented classdef attributes</title>
		<link>https://undocumentedmatlab.com/articles/undocumented-classdef-attributes?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=undocumented-classdef-attributes</link>
					<comments>https://undocumentedmatlab.com/articles/undocumented-classdef-attributes#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 21 Nov 2012 18:00:54 +0000</pubDate>
				<category><![CDATA[Medium risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[MCOS]]></category>
		<category><![CDATA[Object oriented]]></category>
		<category><![CDATA[Pure Matlab]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3315</guid>

					<description><![CDATA[<p>Matlab's object-oriented class definition enables usage of some useful undocumented attributes. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/undocumented-classdef-attributes">Undocumented classdef attributes</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/getundoc-get-undocumented-object-properties" rel="bookmark" title="getundoc &#8211; get undocumented object properties">getundoc &#8211; get undocumented object properties </a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-hg2-graphics-events" rel="bookmark" title="Undocumented HG2 graphics events">Undocumented HG2 graphics events </a> <small>Matlab's new HG2 graphics engine includes many new undocumented events that could be used in various ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-scatter-plot-jitter" rel="bookmark" title="Undocumented scatter plot jitter">Undocumented scatter plot jitter </a> <small>Matlab's scatter plot can automatically jitter data to enable better visualization of distribution density. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/more-undocumented-timing-features" rel="bookmark" title="More undocumented timing features">More undocumented timing features </a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Matlab&#8217;s new (or should I say &#8220;renewed&#8221;) <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/object-oriented-programming.html">object-oriented paradigm</a>, uses attributes to define class behavior. Not surprisingly, some attributes used by Matlab&#8217;s internal classes are not documented in the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/matlab_oop/class-attributes.html">official documentation</a> of the supported attributes. These attributes could well be useful when defining our custom classes:</p>
<h3 id="MCOS">MCOS (Matlab Common/Class Object System) classes</h3>
<table>
<tr>
<th>Attribute Name</th>
<th>Class</th>
<th>Default value if unspecified</th>
<th>Description</th>
</tr>
<tr>
<td><code>CaseInsensitiveProperties</code></td>
<td><code>logical (true/false)</code></td>
<td><code>false</code></td>
<td>If <code>true</code>, then such properties can be accessed using any case. For example, if the property is called <code>myProp</code>, then it can be called as <code>myclass.myProp</code> or <code>myClass.MYPROP</code> or any other variation.<br />
If the class inherits <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/ref/hgsetget.html"><i><b>hgsetget</b></i></a>, then the property can also be used in this way within <i><b>get/set</b></i> functions:</p>
<pre lang='matlab'>get(myClass,'MYPROP')</pre>
<p>If the attribute is unspecified or <code>false</code>, then trying to use a case-insensitive version of the property name would result in an error:<br />
<font color="red"></p>
<pre>Error using MyClass/get
Property MYPROP not found in class MyClass, or is not present in all elements of the array of class MyClass.</pre>
<p></font>
</td>
</tr>
<tr>
<td><code>TruncatedProperties</code></td>
<td><code>logical (true/false)</code></td>
<td><code>false</code></td>
<td>If <code>true</code>, then such properties can be accessed using any substring that leads to a unique property. For example, if the property is called <code>myProperty</code>, then it can be called as <code>myclass.myProperty</code> or <code>myClass.myProp</code> or any other variation.<br />
If the class inherits <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/matlab/ref/hgsetget.html"><i><b>hgsetget</b></i></a>, then the property can also be used in this way within <i><b>get/set</b></i> functions:</p>
<pre lang='matlab'>get(myClass,'myProp')</pre>
<p>Note that if the class has another property called <code>myProblem</code>, then <code>myClass.myPro</code> would be ambiguous and result in an error message; we would need to use <code>myClass.myProp</code> or some other unique term:<br />
<font color="red"></p>
<pre>Error using MyClass/get
Ambiguous MyClass property: 'myPro'.</pre>
<p></font><br />
If the attribute is unspecified or <code>false</code>, then trying to use a truncated version of the property name would result in an error:<br />
<font color="red"></p>
<pre>Error using MyClass/get
Property myProp not found in class MyClass, or is not present in all elements of the array of class MyClass.</pre>
<p></font>
</td>
</tr>
<tr>
<td><code>Enumeration</code></td>
<td><code>logical (true/false)</code></td>
<td><code>false</code></td>
<td>If <code>true</code>, then the class appears to be an enumeration class. I am not exactly sure how this could be useful. I saw references to this attribute in the Data Aquisition Toolbox.
</td>
</tr>
<tr>
<td><code>SupportClassFunctions</code></td>
<td><code>logical (true/false)</code></td>
<td><code>false</code></td>
<td>As above, I am not exactly sure how this could be useful. I saw reference to this attribute in <i>%matlabroot%/toolbox/shared/controllib/engine/</i> (I don&#8217;t currently have ready access to this toolbox so I can&#8217;t test).
</td>
</tr>
<tr>
<td><code>Description</code></td>
<td><code>char</code></td>
<td><code>''</code></td>
<td>A string that describes the class. First mentioned <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/13819469/description-and-detaileddescription-attributes-of-matlab-classes">here</a>.
</td>
</tr>
<tr>
<td><code>DetailedDescription</code></td>
<td><code>char</code></td>
<td><code>''</code></td>
<td>A string that describes the class in more detail. First mentioned <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/13819469/description-and-detaileddescription-attributes-of-matlab-classes">here</a>.
</td>
</tr>
</table>
<p>The <code>CaseInsensitiveProperties</code> and <code>TruncatedProperties</code> attributes are <code>true</code> for Matlab HG (Handle-Graphics) properties. Many users have come to rely on the fact that they can specify the <b>Position</b> property using <code>get(gcf,'position')</code> or <code>set(gcf,'Pos')</code>. It is very intuitive to use. For this reason I do not understand why MathWorks chose to set these attributes&#8217; default value to <code>false</code>. At least now we can manually change this for our classes.<br />
<span id="more-3315"></span><br />
Sample usage (see <i>%matlabroot%/toolbox/matlab/graphics/+graphics/+internal/@LinkAxes/LinkAxes.m</i> and many others):</p>
<pre lang='matlab'>
classdef (CaseInsensitiveProperties=true, TruncatedProperties=true, ConstructOnLoad=true) LinkAxes < handle
classdef (CaseInsensitiveProperties, TruncatedProperties, ConstructOnLoad) LinkAxes < handle  % equivalent alternative
</pre>
<p>I could not find any similar undocumented attributes for properties, methods, events and enumerations.<br />
Do you know of any other useful attribute for the class, or for its constituents? If so, please post a short <a href="/articles/undocumented-classdef-attributes#respond">comment below</a>.</p>
<h3 id="UDD">UDD (Universal Data Dictionary, schema) classes</h3>
<p>UDD (schema) classes have the property truncation on by default, and in fact this cannot be unset. I.e., we cannot have a variant of MCOS&#8217;s <code>TruncatedProperties=false</code> for schema objects:</p>
<pre lang='matlab'>
>> hTabGroup = uitabgroup;
>> get(hTabGroup,'FlowDir')  % truncated from 'FlowDirection'
ans =
topdown
</pre>
<p>The properties do have a settable <b>CaseSensitive</b> <a target="_blank" href="/articles/udd-properties/">meta-property</a> that we can set to &#8216;on/off&#8217; (default=&#8217;off&#8217;) on a property-by-property basis. In most cases, we would naturally stick with the default behavior:</p>
<pre lang='matlab'>
>> get(hTabGroup,'flowdir')  % case-insensitive form of 'FlowDirection'
ans =
topdown
</pre>
<p>This mean that by default, schema objects behave similarly to the ubiquitous HG behavior, whereas custom MCOS objects have a default behavior that is much stricter and needs special handling.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/undocumented-classdef-attributes">Undocumented classdef attributes</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/getundoc-get-undocumented-object-properties" rel="bookmark" title="getundoc &#8211; get undocumented object properties">getundoc &#8211; get undocumented object properties </a> <small>getundoc is a very simple utility that displays the hidden (undocumented) properties of a specified handle object....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-hg2-graphics-events" rel="bookmark" title="Undocumented HG2 graphics events">Undocumented HG2 graphics events </a> <small>Matlab's new HG2 graphics engine includes many new undocumented events that could be used in various ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/undocumented-scatter-plot-jitter" rel="bookmark" title="Undocumented scatter plot jitter">Undocumented scatter plot jitter </a> <small>Matlab's scatter plot can automatically jitter data to enable better visualization of distribution density. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/more-undocumented-timing-features" rel="bookmark" title="More undocumented timing features">More undocumented timing features </a> <small>There are several undocumented ways in Matlab to get CPU and clock data...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/undocumented-classdef-attributes/feed</wfw:commentRss>
			<slash:comments>20</slash:comments>
		
		
			</item>
	</channel>
</rss>
