<?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: More MATLAB Gotchas</title>
	<atom:link href="http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-32269</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 19 May 2011 19:08:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-32269</guid>
		<description>Ben-

I am not able to explain that behavior. PLEASE contact technical support (link on right of my blog) with code and data to reproduce the issue.  Thanks!

--Loren</description>
		<content:encoded><![CDATA[<p>Ben-</p>
<p>I am not able to explain that behavior. PLEASE contact technical support (link on right of my blog) with code and data to reproduce the issue.  Thanks!</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-32268</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Thu, 19 May 2011 18:58:14 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-32268</guid>
		<description>Hi Loren,

I recently saw some behavior I am unable to understand given what you said above (#10).

(I&#039;m a few years late with this comment, but since I&#039;m using R2007a, I figured this would be a good place to post.  If I&#039;m mistaken, please let me know where this question should be asked.)

I have a function that loads some .mat files and then assigns that data to a certain structure format and then returns the new structure.  Essentially, the function looks like this:

&lt;pre&gt;
function out1 = MyFunc(path);

files = dir(path);
for fdex=3:size(files,1) %skip the &#039;.&#039; and &#039;..&#039; entries
    load_me = strcat(path, files(fdex).name);
    data = load(load_me); %the actual data-loading step
    struct(fdex).a = data.b;
    struct(fdex).b = data.f;
end
    out1 = struct;
end
&lt;/pre&gt;
(I realize struct and out1 are redundant variables, but the strange behavior only appears if it&#039;s done this way.)  Once the function returns, I have a variable in my main workspace.  Since there are multiple files I loop through, the physical memory on my system is just about maxed (3.5G out of 4G) after the function completes.  If I save and clear that variable (or clear all), the workspace is empty.  However, the physical memory on my system stays unchanged (3.5G/4G). If I change the code to read:
&lt;pre&gt;
function out1 = MyFunc(path);
...
    out1 = struct;
    clear struct
end
&lt;/pre&gt;
 or if I don&#039;t use the redundant variable:
&lt;pre&gt;
function struct = MyFunc(path);
... %no mention of out1 anywhere
end
&lt;/pre&gt;
then the physical memory used on my computer drops (as expected) once I clear the variable.  

Are you able to explain this behavior?  It seems that MATLAB does not clear all the function variables when the function exits?  (I&#039;m using Windows 7 and I&#039;ve used both Process Explorer and Task Manager to look at my system memory.)</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I recently saw some behavior I am unable to understand given what you said above (#10).</p>
<p>(I&#8217;m a few years late with this comment, but since I&#8217;m using R2007a, I figured this would be a good place to post.  If I&#8217;m mistaken, please let me know where this question should be asked.)</p>
<p>I have a function that loads some .mat files and then assigns that data to a certain structure format and then returns the new structure.  Essentially, the function looks like this:</p>
<pre>
function out1 = MyFunc(path);

files = dir(path);
for fdex=3:size(files,1) %skip the '.' and '..' entries
    load_me = strcat(path, files(fdex).name);
    data = load(load_me); %the actual data-loading step
    struct(fdex).a = data.b;
    struct(fdex).b = data.f;
end
    out1 = struct;
end
</pre>
<p>(I realize struct and out1 are redundant variables, but the strange behavior only appears if it&#8217;s done this way.)  Once the function returns, I have a variable in my main workspace.  Since there are multiple files I loop through, the physical memory on my system is just about maxed (3.5G out of 4G) after the function completes.  If I save and clear that variable (or clear all), the workspace is empty.  However, the physical memory on my system stays unchanged (3.5G/4G). If I change the code to read:</p>
<pre>
function out1 = MyFunc(path);
...
    out1 = struct;
    clear struct
end
</pre>
<p> or if I don&#8217;t use the redundant variable:</p>
<pre>
function struct = MyFunc(path);
... %no mention of out1 anywhere
end
</pre>
<p>then the physical memory used on my computer drops (as expected) once I clear the variable.  </p>
<p>Are you able to explain this behavior?  It seems that MATLAB does not clear all the function variables when the function exits?  (I&#8217;m using Windows 7 and I&#8217;ve used both Process Explorer and Task Manager to look at my system memory.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-22915</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Sun, 09 Dec 2007 15:24:13 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-22915</guid>
		<description>Christophe-

The function workspace will be cleaned up when you exit the function so there is no need to manually clear the variables.  If you have a large variable that has changed in the middle of the function and you want to do a lot more before returning from the function, clearing the large variable *may* be beneficial.

--Loren</description>
		<content:encoded><![CDATA[<p>Christophe-</p>
<p>The function workspace will be cleaned up when you exit the function so there is no need to manually clear the variables.  If you have a large variable that has changed in the middle of the function and you want to do a lot more before returning from the function, clearing the large variable *may* be beneficial.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christophe</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-22885</link>
		<dc:creator>Christophe</dc:creator>
		<pubDate>Sun, 09 Dec 2007 00:10:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-22885</guid>
		<description>Hi Loren,
Is it useful to clear local variables inside function when they are huge?
I see the point of freeing memory when working in the base workspace but MATLAB is freeing local variables when exiting a function, right?
So what&#039;s the need of clearing them at the end of the function. Is there any speed gain in execution? 
Keep up the good work.
Christophe</description>
		<content:encoded><![CDATA[<p>Hi Loren,<br />
Is it useful to clear local variables inside function when they are huge?<br />
I see the point of freeing memory when working in the base workspace but MATLAB is freeing local variables when exiting a function, right?<br />
So what&#8217;s the need of clearing them at the end of the function. Is there any speed gain in execution?<br />
Keep up the good work.<br />
Christophe</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cara</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-20523</link>
		<dc:creator>Cara</dc:creator>
		<pubDate>Tue, 06 Nov 2007 14:08:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-20523</guid>
		<description>I just discovered this too. It is undocumented and ridiculous. But how do you add something to the classpath without clearing everything?</description>
		<content:encoded><![CDATA[<p>I just discovered this too. It is undocumented and ridiculous. But how do you add something to the classpath without clearing everything?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rodney</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-16194</link>
		<dc:creator>Rodney</dc:creator>
		<pubDate>Tue, 24 Apr 2007 06:12:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-16194</guid>
		<description>One Gotcha that got me recently was the use of &#039;javaaddpath&#039;.

The code was something as follows

function foo()

  javaaddpath(&#039;\\share\cat_code\java&#039;);

  global cat;

  if (cat == &#039;siamese&#039;)
    fprintf(1, &#039;Meow\n&#039;);
  end

end

The problem that would occur is that cat did not seemed to be picked up as a global and hence the condition was never triggered.

I eventually traced it down to the javaaddpath command, which calls javaclasspath which makes a call to &quot;clear(&#039;java&#039;)&quot;.

Looking into the documentation :

clear java    The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path

.... oh... so I just did clear all... fantastic! Just what I expected to happen when I add an entry to the Java path.</description>
		<content:encoded><![CDATA[<p>One Gotcha that got me recently was the use of &#8216;javaaddpath&#8217;.</p>
<p>The code was something as follows</p>
<p>function foo()</p>
<p>  javaaddpath(&#8216;\\share\cat_code\java&#8217;);</p>
<p>  global cat;</p>
<p>  if (cat == &#8216;siamese&#8217;)<br />
    fprintf(1, &#8216;Meow\n&#8217;);<br />
  end</p>
<p>end</p>
<p>The problem that would occur is that cat did not seemed to be picked up as a global and hence the condition was never triggered.</p>
<p>I eventually traced it down to the javaaddpath command, which calls javaclasspath which makes a call to &#8220;clear(&#8216;java&#8217;)&#8221;.</p>
<p>Looking into the documentation :</p>
<p>clear java    The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path</p>
<p>&#8230;. oh&#8230; so I just did clear all&#8230; fantastic! Just what I expected to happen when I add an entry to the Java path.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Salloker</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-16135</link>
		<dc:creator>Michael Salloker</dc:creator>
		<pubDate>Thu, 15 Mar 2007 12:47:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-16135</guid>
		<description>Hello Loren,

a very common mistake is using i or j as an index in a for loop. After that calculations with a complex nuumbers give often an unexpecting result. 

The reason is the same like in your examplex, but you will not receive an error message, which makes this a little bit more dangerous.

Michael</description>
		<content:encoded><![CDATA[<p>Hello Loren,</p>
<p>a very common mistake is using i or j as an index in a for loop. After that calculations with a complex nuumbers give often an unexpecting result. </p>
<p>The reason is the same like in your examplex, but you will not receive an error message, which makes this a little bit more dangerous.</p>
<p>Michael</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naor</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-16128</link>
		<dc:creator>Naor</dc:creator>
		<pubDate>Fri, 09 Mar 2007 18:48:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-16128</guid>
		<description>btw, the answer was that mlock protects one file, while the same global variable can be defined in many files. you can see the post in the newsgroup archive:
http://newsreader.mathworks.com/WebX/.ef38ad4?50@382.HITabyR53yv@</description>
		<content:encoded><![CDATA[<p>btw, the answer was that mlock protects one file, while the same global variable can be defined in many files. you can see the post in the newsgroup archive:<br />
<a href="http://newsreader.mathworks.com/WebX/.ef38ad4?50@382.HITabyR53yv@" rel="nofollow">http://newsreader.mathworks.com/WebX/.ef38ad4?50@382.HITabyR53yv@</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naor</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-16126</link>
		<dc:creator>Naor</dc:creator>
		<pubDate>Fri, 09 Mar 2007 12:54:08 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-16126</guid>
		<description>a response to myself: A search in the newsgroup archive revealed that I had already asked this question there, and Loren had answered it! I completely forgot about it, the human brain is so unreliable... Thanks Loren</description>
		<content:encoded><![CDATA[<p>a response to myself: A search in the newsgroup archive revealed that I had already asked this question there, and Loren had answered it! I completely forgot about it, the human brain is so unreliable&#8230; Thanks Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naor</title>
		<link>http://blogs.mathworks.com/loren/2007/03/07/more-matlab-gotchas/#comment-16125</link>
		<dc:creator>Naor</dc:creator>
		<pubDate>Fri, 09 Mar 2007 12:46:31 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=80#comment-16125</guid>
		<description>About mlock and persistent. I was trying to protect a global variable from resetting by using mlock, but this doesn&#039;t work. And you can&#039;t define a variable as both global and persistent. Is there a way around this. (I&#039;ll also ask the newsgroup.)</description>
		<content:encoded><![CDATA[<p>About mlock and persistent. I was trying to protect a global variable from resetting by using mlock, but this doesn&#8217;t work. And you can&#8217;t define a variable as both global and persistent. Is there a way around this. (I&#8217;ll also ask the newsgroup.)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

