<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155519</link>
    <title>MATLAB Central Newsreader - Re: What am I doing wrong here</title>
    <description>Feed for thread: Re: What am I doing wrong here</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 02 Sep 2007 14:33:00 -0400</pubDate>
      <title>Re: What am I doing wrong here</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/155519#390454</link>
      <author>Per Isakson</author>
      <description>"Markus Buehren" &amp;lt;mb_REMOVEmatlab@gmxTHIS.de&amp;gt; wrote in &lt;br&gt;
message &amp;lt;fbcb4m$9no$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi Doug!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for this perfect example for NOT using nested &lt;br&gt;
functions!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards&lt;br&gt;
&amp;gt; Markus&lt;br&gt;
&lt;br&gt;
I want to comment on this conclusion since I &lt;br&gt;
think "closures" (&lt;a href="http://en.wikipedia.org/wiki/Closure_%"&gt;http://en.wikipedia.org/wiki/Closure_%&lt;/a&gt;&lt;br&gt;
28computer_science%29, 2007-09-01) realized with function &lt;br&gt;
handles and nested functions is the best feature added to &lt;br&gt;
Matlab since "function". It's actually better than sliced &lt;br&gt;
bread.&lt;br&gt;
&lt;br&gt;
The original post in this thread reads:&lt;br&gt;
&lt;br&gt;
Jason Ingram 2007-04-24 wrote:&lt;br&gt;
&lt;br&gt;
I am trying to create an object by using a struct: &lt;br&gt;
&lt;br&gt;
function obj=testMe() &lt;br&gt;
&amp;nbsp;&amp;nbsp;obj.a=4; &lt;br&gt;
&amp;nbsp;&amp;nbsp;obj.getA = @getA; &lt;br&gt;
&amp;nbsp;&amp;nbsp;obj.setA = @setA; &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;function val=getA() &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;val=obj.a; &lt;br&gt;
&amp;nbsp;end &lt;br&gt;
&lt;br&gt;
function setA(val) &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.a=val; &lt;br&gt;
&amp;nbsp;&amp;nbsp;end &lt;br&gt;
end &lt;br&gt;
&lt;br&gt;
And here are the results: &lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; a=testMe; &lt;br&gt;
&amp;gt;&amp;gt; a.getA() &lt;br&gt;
&lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4 &lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; a.setA(10); &lt;br&gt;
&amp;gt;&amp;gt; a.a &lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4 &lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; a.getA() &lt;br&gt;
ans = &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;10 &lt;br&gt;
&lt;br&gt;
Why are the two values different? Where is the second &lt;br&gt;
variable stored? &lt;br&gt;
&lt;br&gt;
I am not too familiar with Matlab and I have 400 lines of &lt;br&gt;
tight code with complex logic written in a similar way with &lt;br&gt;
nested classes, etc. I badly need to resolve this issue. &lt;br&gt;
Can somebody help? &lt;br&gt;
&amp;lt;end original post&amp;gt;&lt;br&gt;
&lt;br&gt;
The short answere is: It is a mistake to (try to) make the &lt;br&gt;
variable "a" public as part of the structure (i.e. as a.a).&lt;br&gt;
&lt;br&gt;
myobj = testMe()&lt;br&gt;
&lt;br&gt;
creates a structure, which obeys the Matlab rules of copy-&lt;br&gt;
by-value. The values of two fields of the structure, myobj, &lt;br&gt;
are function handles of nested functions, which in turn &lt;br&gt;
share a scope(/workspace). These function handles contain &lt;br&gt;
*references* to variables in the main function, testMe. The &lt;br&gt;
function handles are references, i.e. a copy of a function &lt;br&gt;
handle refers to the same underlying "stuff". The details &lt;br&gt;
on the scope are in the documentation. &lt;br&gt;
&lt;br&gt;
The assignment&lt;br&gt;
&lt;br&gt;
myobj.a = 10; &lt;br&gt;
&lt;br&gt;
"triggers" the copy-by-value-rules and makes myobj.a refer &lt;br&gt;
to another piece of memory and thus the connection with &lt;br&gt;
obj.a in testMe is broken.&lt;br&gt;
&lt;br&gt;
The conclusion is that access to the state of the object &lt;br&gt;
must be done with the help of function handles to nested &lt;br&gt;
functions.&lt;br&gt;
&lt;br&gt;
These "closures" are a bit frustrating. They allow us to do &lt;br&gt;
a bit of object oriented stuff, but just a bit and we have &lt;br&gt;
to find out about the limitaions ourself. BTW, I couldn't &lt;br&gt;
see any sign of Matlabs new class-system in the release &lt;br&gt;
note of R2007b. &lt;br&gt;
&lt;br&gt;
Reading: &lt;br&gt;
1. &amp;lt;Loren on the Art of MATLAB&amp;gt;, August 9th, 2007, A Way to &lt;br&gt;
Create Reusable Tools, specially the comment by &lt;br&gt;
Michael Corvin replied on August 16th, 2007 at 11:28 pm : &lt;br&gt;
&lt;br&gt;
There is more to find i Lorens blogs, but it takes some &lt;br&gt;
time to find it.&lt;br&gt;
&lt;br&gt;
2. cssm (this newsgroup), thread: THIS (or SELF) object in &lt;br&gt;
matlab, 2005-11-23. Tom Krauss proposes a clever construct &lt;br&gt;
that allows us to write: myobf.a = 10; &lt;br&gt;
&lt;br&gt;
A quick fix to testMe would be: &lt;br&gt;
&lt;br&gt;
function obj=testMe() &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;obj.getA = @getA; &lt;br&gt;
&amp;nbsp;&amp;nbsp;obj.setA = @setA; &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;state.a = 4;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;function val=getA() &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;val=state.a; &lt;br&gt;
&amp;nbsp;end &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;function setA(val) &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;state.a=val; &lt;br&gt;
&amp;nbsp;&amp;nbsp;end &lt;br&gt;
end &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
There are undocumented stuff in Matlab (e.g. schema, &lt;br&gt;
handle) that make it possible to take this further. See the &lt;br&gt;
code of memmapfile. But that's for experts.&lt;br&gt;
&lt;br&gt;
/ per&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
