<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Solving a Matlab MCOS bug	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=solving-a-matlab-mcos-bug</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Thu, 07 Jan 2016 11:35:18 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Jeroen Boschma		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-366968</link>

		<dc:creator><![CDATA[Jeroen Boschma]]></dc:creator>
		<pubDate>Thu, 07 Jan 2016 11:35:18 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-366968</guid>

					<description><![CDATA[Hi Yair,

I think this is certainly not a simple MCOS parser bug, even not a bug at all, but since I have limited knowledge about the inner things of MATLAB classes I may be on thin ice here...

Try the following as your very first statement:

&lt;pre lang=&quot;matlab&quot;&gt; 
MainClass.inner.value2
ans =
     []
&lt;/pre&gt;

So that rules out that there is only a simple MCOS parser bug just because there would be too many dereferencing levels. Since there is no variable MainClass, MATLAB correctly dereferences all levels within the class MainClass.

The underlying problem, I think, is that you cannot change class definitions, only instances of classes. What in this case happens is the following:

1) Within &#039;setInnerValue()&#039; there is no variable named MainClass, and MATLAB detects the reference to the class, correctly taking all reference levels into account (my example above shows that this is not the problem).
2) MATLAB detects the assignment statement.
3) MATLAB falls back on the rule &lt;i&gt;&quot;modifying class definitions (i.e. its properties) is not possible&quot;.&lt;/i&gt;
4) MATLAB falls back on the only remaining option: create a new struct variable &#039;MainClass&#039; and execute the assignment.

So I think the bottom line is that MATLAB has a strict interpretation of &lt;i&gt;&quot;modifying class definitions is not possible&quot;&lt;/i&gt;, and that therefore every direct assignment via the class definition is prohibited. This is not a bug at all in my opinion, but you end up in a discussion how tight the underlying fields of &#039;inner&#039; are related to MainClass itself. The fact that you can change the fields of &#039;inner&#039; using a workaround does not mean that MATLAB should allow doing this directly via the class definition itself. For this matter, I tend to agree with MATLAB&#039;s choice.]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,</p>
<p>I think this is certainly not a simple MCOS parser bug, even not a bug at all, but since I have limited knowledge about the inner things of MATLAB classes I may be on thin ice here&#8230;</p>
<p>Try the following as your very first statement:</p>
<pre lang="matlab"> 
MainClass.inner.value2
ans =
     []
</pre>
<p>So that rules out that there is only a simple MCOS parser bug just because there would be too many dereferencing levels. Since there is no variable MainClass, MATLAB correctly dereferences all levels within the class MainClass.</p>
<p>The underlying problem, I think, is that you cannot change class definitions, only instances of classes. What in this case happens is the following:</p>
<p>1) Within &#8216;setInnerValue()&#8217; there is no variable named MainClass, and MATLAB detects the reference to the class, correctly taking all reference levels into account (my example above shows that this is not the problem).<br />
2) MATLAB detects the assignment statement.<br />
3) MATLAB falls back on the rule <i>&#8220;modifying class definitions (i.e. its properties) is not possible&#8221;.</i><br />
4) MATLAB falls back on the only remaining option: create a new struct variable &#8216;MainClass&#8217; and execute the assignment.</p>
<p>So I think the bottom line is that MATLAB has a strict interpretation of <i>&#8220;modifying class definitions is not possible&#8221;</i>, and that therefore every direct assignment via the class definition is prohibited. This is not a bug at all in my opinion, but you end up in a discussion how tight the underlying fields of &#8216;inner&#8217; are related to MainClass itself. The fact that you can change the fields of &#8216;inner&#8217; using a workaround does not mean that MATLAB should allow doing this directly via the class definition itself. For this matter, I tend to agree with MATLAB&#8217;s choice.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363266</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 04 Dec 2015 11:16:40 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363266</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363265&quot;&gt;alexandre&lt;/a&gt;.

The property is constant in the sense that its reference handle remains unmodified after being set, but its inner properties are not constant. It&#039;s like saying that a company&#039;s ID remains constant, but its employees, phone number, and physical address may possibly change over time. Or like saying that a house address remains constant but its occupants change over time. 

Related post: http://undocumentedmatlab.com/blog/handle-object-as-default-class-property-value]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363265">alexandre</a>.</p>
<p>The property is constant in the sense that its reference handle remains unmodified after being set, but its inner properties are not constant. It&#8217;s like saying that a company&#8217;s ID remains constant, but its employees, phone number, and physical address may possibly change over time. Or like saying that a house address remains constant but its occupants change over time. </p>
<p>Related post: <a href="http://undocumentedmatlab.com/blog/handle-object-as-default-class-property-value" rel="ugc">http://undocumentedmatlab.com/blog/handle-object-as-default-class-property-value</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: alexandre		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363265</link>

		<dc:creator><![CDATA[alexandre]]></dc:creator>
		<pubDate>Fri, 04 Dec 2015 11:10:12 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363265</guid>

					<description><![CDATA[cool post!

I am confused about the following point:

The inner class is declared inside the &#039;constant&#039; properties - why would we be assigning to it in the first place? It seems to me that this is key for a problem to occur (there is no other attribute that I can see that could be accessed via the static function call - http://se.mathworks.com/help/matlab/matlab_oop/property-attributes.html)

Reading the docs above suggeststhat Constant properties can be modified (but not by sub-classes). But this article suggests  that they should be set at definition and not touched thereafter: http://se.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html.

So are matlab (Constant) properties what other OO languages call &#039;static&#039; properties?]]></description>
			<content:encoded><![CDATA[<p>cool post!</p>
<p>I am confused about the following point:</p>
<p>The inner class is declared inside the &#8216;constant&#8217; properties &#8211; why would we be assigning to it in the first place? It seems to me that this is key for a problem to occur (there is no other attribute that I can see that could be accessed via the static function call &#8211; <a href="http://se.mathworks.com/help/matlab/matlab_oop/property-attributes.html" rel="nofollow ugc">http://se.mathworks.com/help/matlab/matlab_oop/property-attributes.html</a>)</p>
<p>Reading the docs above suggeststhat Constant properties can be modified (but not by sub-classes). But this article suggests  that they should be set at definition and not touched thereafter: <a href="http://se.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html" rel="nofollow ugc">http://se.mathworks.com/help/matlab/matlab_oop/properties-with-constant-values.html</a>.</p>
<p>So are matlab (Constant) properties what other OO languages call &#8216;static&#8217; properties?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dave Foti		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363168</link>

		<dc:creator><![CDATA[Dave Foti]]></dc:creator>
		<pubDate>Thu, 03 Dec 2015 14:22:29 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363168</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363088&quot;&gt;Yair Altman&lt;/a&gt;.

Hi Yair,
The problem is that it isn&#039;t enough for &quot;MainClass = 1&quot; to ensure that MainClass is a variable.  As long as structs have existed in MATLAB, it has been possible to create structs using the syntax &quot;MainClass.inner = 1&quot; just as numeric arrays can be created using indexing as in &quot;X(1:10) = 1&quot;  Thus, your proposed rules would still mean that a function or script (like the 2 createStruct examples I provided) could be written using local struct variables that later got re-interpreted as referring to a class and its property that happened to be on the path.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363088">Yair Altman</a>.</p>
<p>Hi Yair,<br />
The problem is that it isn&#8217;t enough for &#8220;MainClass = 1&#8221; to ensure that MainClass is a variable.  As long as structs have existed in MATLAB, it has been possible to create structs using the syntax &#8220;MainClass.inner = 1&#8221; just as numeric arrays can be created using indexing as in &#8220;X(1:10) = 1&#8221;  Thus, your proposed rules would still mean that a function or script (like the 2 createStruct examples I provided) could be written using local struct variables that later got re-interpreted as referring to a class and its property that happened to be on the path.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363134</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Thu, 03 Dec 2015 07:16:25 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363134</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363118&quot;&gt;Brad Stiritz&lt;/a&gt;.

@Brad - indeed so :-)]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363118">Brad Stiritz</a>.</p>
<p>@Brad &#8211; indeed so 🙂</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Brad Stiritz		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363118</link>

		<dc:creator><![CDATA[Brad Stiritz]]></dc:creator>
		<pubDate>Thu, 03 Dec 2015 04:02:04 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363118</guid>

					<description><![CDATA[Hi Yair, thanks for a super-interesting post. Thanks to Dave as well for the helpful clarification. Just to clarify your workaround, Yair, here&#039;s a revised implementation of the static function, and the expected result 
produced by the new code:

&lt;pre lang=&quot;matlab&quot;&gt;
classdef (Abstract) MainClass
    properties (Constant)
        inner = InnerClass
    end
    methods (Static)
        function setInnerValue(newValue)
            innerObj = MainClass.inner;
            innerObj.value1 = newValue;
        end
    end
end
&lt;/pre&gt;

&lt;pre lang=&#039;matlab&#039;&gt;
&gt;&gt; MainClass.setInnerValue(5)
&gt;&gt; MainClass.inner
ans = 
  InnerClass with properties:
    value1: 5
    value2: []
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Hi Yair, thanks for a super-interesting post. Thanks to Dave as well for the helpful clarification. Just to clarify your workaround, Yair, here&#8217;s a revised implementation of the static function, and the expected result<br />
produced by the new code:</p>
<pre lang="matlab">
classdef (Abstract) MainClass
    properties (Constant)
        inner = InnerClass
    end
    methods (Static)
        function setInnerValue(newValue)
            innerObj = MainClass.inner;
            innerObj.value1 = newValue;
        end
    end
end
</pre>
<pre lang='matlab'>
>> MainClass.setInnerValue(5)
>> MainClass.inner
ans = 
  InnerClass with properties:
    value1: 5
    value2: []
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363088</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 02 Dec 2015 22:21:23 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363088</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363083&quot;&gt;Dave Foti&lt;/a&gt;.

@Dave - thanks for your clarification, but the inconsistency here between the assignment and reference precedence rules is confusing. I contend that the following precedence rule might be more intuitive and consitent in such cases, while still preserving the default behavior for the remainder of the cases:

   1. try to understand MainClass (in both assignment and referencing) as referring to variable MainClass on the local workspace. If successful then continue to use this variable.
   2. otherwise, try to understand MainClass as a class/package name. If successful and no error, then continue to use this. Note that this does not break your requirement for MainClass=5 to override the class definition and create a local variable named MainClass, because assigning to a class is illegal and therefore raises an error and falls-through to the following rule:
   3. otherwise, in an assignment create a new workspace variable named MainClass, and in a reference raise an error about trying to use an unrecognized identifier.

These precedence rules will ensure that variables always take precedence over function/class/package names (as you have rightfully pointed out), but only when they exist - otherwise the existing function/class/package name should obviously take precedence over the non-existing variable. The behavior now becomes fully consistent across assignment and reference, and I think is much more intuitive in run-time than the current situation.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363083">Dave Foti</a>.</p>
<p>@Dave &#8211; thanks for your clarification, but the inconsistency here between the assignment and reference precedence rules is confusing. I contend that the following precedence rule might be more intuitive and consitent in such cases, while still preserving the default behavior for the remainder of the cases:</p>
<p>   1. try to understand MainClass (in both assignment and referencing) as referring to variable MainClass on the local workspace. If successful then continue to use this variable.<br />
   2. otherwise, try to understand MainClass as a class/package name. If successful and no error, then continue to use this. Note that this does not break your requirement for MainClass=5 to override the class definition and create a local variable named MainClass, because assigning to a class is illegal and therefore raises an error and falls-through to the following rule:<br />
   3. otherwise, in an assignment create a new workspace variable named MainClass, and in a reference raise an error about trying to use an unrecognized identifier.</p>
<p>These precedence rules will ensure that variables always take precedence over function/class/package names (as you have rightfully pointed out), but only when they exist &#8211; otherwise the existing function/class/package name should obviously take precedence over the non-existing variable. The behavior now becomes fully consistent across assignment and reference, and I think is much more intuitive in run-time than the current situation.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dave Foti		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363083</link>

		<dc:creator><![CDATA[Dave Foti]]></dc:creator>
		<pubDate>Wed, 02 Dec 2015 21:12:20 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363083</guid>

					<description><![CDATA[I can understand Yair&#039;s confusion, but there is a simple explanation the behavior described in this post (with Constant properties).  MATLAB doesn&#039;t have variable declarations.  When a name appears on the left of an equal sign, that statement always create a variable from the left-most name.  Try &quot;MainClass.inner = 1&quot; and you will see that it doesn&#039;t matter how complex the indexing expression is, assignment always introduces a new variable that hides any function or class of the same name.  In this case the dot indexing means that MainClass will be a struct.  It is important that variables have highest precedence so that scripts and functions don&#039;t have to worry about collisions between the variable names used in the code and functions and classes on the path (a set of names that is very large and always growing).

Reference to a name that has not been introduced as a variable will find the class.  It doesn&#039;t matter how many dots you have or how complex the indexing expression is.  Here are some functions you can try that show how MATLAB consistently treats assignment as creating a variable.  Note that the last two functions produce the same results whether or not MainClass exists on the path.  

&lt;pre lang=&quot;matlab&quot;&gt; 
function useClassConstants
   X = MainClass.inner  % MainClass is not a variable, so find inner property of MainClass class
   Y = MainClass.inner.value1 
end
&lt;/pre&gt;
&lt;pre lang=&quot;matlab&quot;&gt; 
function createStruct1
   MainClass.inner = 2 % Create a struct variable named MainClass
   isa(MainClass, &#039;struct&#039;)
end
&lt;/pre&gt;
&lt;pre lang=&quot;matlab&quot;&gt; 
function createStruct2
   MainClass.inner.value1 = 3.5 % Create a struct variable named MainClass
   isequal(MainClass.inner.value1, 3.5) % See that we are only using this struct
end
&lt;/pre&gt;

This comes down to the fact that preserving the meaning of variables independent of what is on the path is considered more important than making it easy to create mutable per-class state (which is often problematic anyway because it can lead to unwanted cross-talk between applications that share the same libraries).]]></description>
			<content:encoded><![CDATA[<p>I can understand Yair&#8217;s confusion, but there is a simple explanation the behavior described in this post (with Constant properties).  MATLAB doesn&#8217;t have variable declarations.  When a name appears on the left of an equal sign, that statement always create a variable from the left-most name.  Try &#8220;MainClass.inner = 1&#8221; and you will see that it doesn&#8217;t matter how complex the indexing expression is, assignment always introduces a new variable that hides any function or class of the same name.  In this case the dot indexing means that MainClass will be a struct.  It is important that variables have highest precedence so that scripts and functions don&#8217;t have to worry about collisions between the variable names used in the code and functions and classes on the path (a set of names that is very large and always growing).</p>
<p>Reference to a name that has not been introduced as a variable will find the class.  It doesn&#8217;t matter how many dots you have or how complex the indexing expression is.  Here are some functions you can try that show how MATLAB consistently treats assignment as creating a variable.  Note that the last two functions produce the same results whether or not MainClass exists on the path.  </p>
<pre lang="matlab"> 
function useClassConstants
   X = MainClass.inner  % MainClass is not a variable, so find inner property of MainClass class
   Y = MainClass.inner.value1 
end
</pre>
<pre lang="matlab"> 
function createStruct1
   MainClass.inner = 2 % Create a struct variable named MainClass
   isa(MainClass, 'struct')
end
</pre>
<pre lang="matlab"> 
function createStruct2
   MainClass.inner.value1 = 3.5 % Create a struct variable named MainClass
   isequal(MainClass.inner.value1, 3.5) % See that we are only using this struct
end
</pre>
<p>This comes down to the fact that preserving the meaning of variables independent of what is on the path is considered more important than making it easy to create mutable per-class state (which is often problematic anyway because it can lead to unwanted cross-talk between applications that share the same libraries).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sven		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363070</link>

		<dc:creator><![CDATA[Sven]]></dc:creator>
		<pubDate>Wed, 02 Dec 2015 19:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363070</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363069&quot;&gt;Sven&lt;/a&gt;.

And to clarify: the above bug was limited to &quot;cell-typed variables of *tables*&quot;.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363069">Sven</a>.</p>
<p>And to clarify: the above bug was limited to &#8220;cell-typed variables of *tables*&#8221;.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sven		</title>
		<link>https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug#comment-363069</link>

		<dc:creator><![CDATA[Sven]]></dc:creator>
		<pubDate>Wed, 02 Dec 2015 19:24:18 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6100#comment-363069</guid>

					<description><![CDATA[There was something like this bug in R2015a, which seems to have been fixed in R2015b.
In order to access the elements of a cell-type variable, you would first need to store a copy of that variable locally.
&lt;pre lang=&quot;matlab&quot;&gt;
T = cell2table({&#039;a&#039;;&#039;b&#039;;&#039;c&#039;},&#039;Var&#039;,{&#039;myCell&#039;})
sprintf(&#039;%s,&#039;,T.myCell{:})
ans =
a,b,c,
&lt;/pre&gt;

The above now works in 2015b. In 2015a however, the result was:
&lt;pre lang=&quot;matlab&quot;&gt;
sprintf(&#039;%s,&#039;,T.myCell{:})
ans =
a,
&lt;/pre&gt;

Whereas you could work around with:
&lt;pre lang=&quot;matlab&quot;&gt;
tmpCell = T.myCell;
sprintf(&#039;%s,&#039;,tmpCell{:})
ans =
a,b,c,
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>There was something like this bug in R2015a, which seems to have been fixed in R2015b.<br />
In order to access the elements of a cell-type variable, you would first need to store a copy of that variable locally.</p>
<pre lang="matlab">
T = cell2table({'a';'b';'c'},'Var',{'myCell'})
sprintf('%s,',T.myCell{:})
ans =
a,b,c,
</pre>
<p>The above now works in 2015b. In 2015a however, the result was:</p>
<pre lang="matlab">
sprintf('%s,',T.myCell{:})
ans =
a,
</pre>
<p>Whereas you could work around with:</p>
<pre lang="matlab">
tmpCell = T.myCell;
sprintf('%s,',tmpCell{:})
ans =
a,b,c,
</pre>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
