<?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: Trapping warnings efficiently	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=trapping-warnings-efficiently</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Fri, 14 Jun 2024 07:39:05 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.3</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-524514</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 14 Jun 2024 07:39:05 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-524514</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-524499&quot;&gt;Bob Pownalll&lt;/a&gt;.

@Bob - you are mistaken: &lt;code&gt;s=warning(...)&lt;/code&gt; returns the warnings state as it was &lt;strong&gt;before&lt;/strong&gt; the state was modified, so my code is correct. There is no need for a separate call to &lt;code&gt;s=warning;&lt;/code&gt;.
I agree that this is not clearly explained in the official doc-page, but you can see the behavior in the example that is provided in that doc-page: &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/warning.html#buh8kqc-13&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;https://www.mathworks.com/help/matlab/ref/warning.html#buh8kqc-13&lt;/a&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-524499">Bob Pownalll</a>.</p>
<p>@Bob &#8211; you are mistaken: <code>s=warning(...)</code> returns the warnings state as it was <strong>before</strong> the state was modified, so my code is correct. There is no need for a separate call to <code>s=warning;</code>.<br />
I agree that this is not clearly explained in the official doc-page, but you can see the behavior in the example that is provided in that doc-page: <a href="https://www.mathworks.com/help/matlab/ref/warning.html#buh8kqc-13" target="_blank" rel="nofollow">https://www.mathworks.com/help/matlab/ref/warning.html#buh8kqc-13</a></p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Bob Pownalll		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-524499</link>

		<dc:creator><![CDATA[Bob Pownalll]]></dc:creator>
		<pubDate>Wed, 12 Jun 2024 13:40:09 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-524499</guid>

					<description><![CDATA[Thanks!  I was trying to figure out how to do exactly this (i.e. Use a try / catch block with something that generated a warning, not an error) and you pointed me to a solution!

I have one question, though.

At the very start of your example, you have:
&lt;code&gt;
% Set a couple of warnings to temporarily issue errors (exceptions)
s = warning(&#039;error&#039;, &#039;MATLAB:DELETE:Permission&#039;);
&lt;/code&gt;

and then at the end,  you have:
&lt;code&gt;
% Restore the warnings back to their previous (non-error) state
warning(s);
&lt;/code&gt;

Won&#039;t this restore the warnings back to the state with the &lt;b&gt;first&lt;/b&gt; warning-converted-to-error, not the &quot;previous (non-error) state&quot;?

To restore the warnings to the previous (non-error) state, shouldn&#039;t your example be:
(Note that the assignment of s has changed.)
&lt;code&gt;
s = warning;
warning(&#039;error&#039;, &#039;MATLAB:DELETE:Permission&#039;);
...
% Restore the warnings back to their previous (non-error) state
warning(s);
&lt;/code&gt;

That seems to be the the way my MATLAB version (R2018a) works.

Or am I misunderstanding something?]]></description>
			<content:encoded><![CDATA[<p>Thanks!  I was trying to figure out how to do exactly this (i.e. Use a try / catch block with something that generated a warning, not an error) and you pointed me to a solution!</p>
<p>I have one question, though.</p>
<p>At the very start of your example, you have:<br />
<code><br />
% Set a couple of warnings to temporarily issue errors (exceptions)<br />
s = warning('error', 'MATLAB:DELETE:Permission');<br />
</code></p>
<p>and then at the end,  you have:<br />
<code><br />
% Restore the warnings back to their previous (non-error) state<br />
warning(s);<br />
</code></p>
<p>Won&#8217;t this restore the warnings back to the state with the <b>first</b> warning-converted-to-error, not the &#8220;previous (non-error) state&#8221;?</p>
<p>To restore the warnings to the previous (non-error) state, shouldn&#8217;t your example be:<br />
(Note that the assignment of s has changed.)<br />
<code><br />
s = warning;<br />
warning('error', 'MATLAB:DELETE:Permission');<br />
...<br />
% Restore the warnings back to their previous (non-error) state<br />
warning(s);<br />
</code></p>
<p>That seems to be the the way my MATLAB version (R2018a) works.</p>
<p>Or am I misunderstanding something?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-514664</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 09 Aug 2022 09:55:19 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-514664</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-514516&quot;&gt;Ken Johnson&lt;/a&gt;.

@Ken - I cannot reproduce your report, it works as expected for me even on R2022b]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-514516">Ken Johnson</a>.</p>
<p>@Ken &#8211; I cannot reproduce your report, it works as expected for me even on R2022b</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Ken Johnson		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-514516</link>

		<dc:creator><![CDATA[Ken Johnson]]></dc:creator>
		<pubDate>Wed, 27 Jul 2022 06:24:54 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-514516</guid>

					<description><![CDATA[Why does the second catch block in this code example not work? (It works if you restart MATLAB and then omit the first try-catch block.)

&lt;pre lang=&quot;Matlab&quot;&gt;
try
    a = [0,0;0,0]\[1;1];
catch
    disp(&#039;Caught.&#039;)
end
lastwarn(&#039;&#039;)
warning(&#039;error&#039;,&#039;MATLAB:singularMatrix&#039;)
try
    a = [0,0;0,0]\[1;1];
catch
    disp(&#039;Caught.&#039;)
end
lasterr
lastwarn
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>Why does the second catch block in this code example not work? (It works if you restart MATLAB and then omit the first try-catch block.)</p>
<pre lang="Matlab">
try
    a = [0,0;0,0]\[1;1];
catch
    disp('Caught.')
end
lastwarn('')
warning('error','MATLAB:singularMatrix')
try
    a = [0,0;0,0]\[1;1];
catch
    disp('Caught.')
end
lasterr
lastwarn
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-359958</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Tue, 27 Oct 2015 16:15:03 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-359958</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-359893&quot;&gt;Noam G&lt;/a&gt;.

@Noam - I am not aware of any way to achieve this. But I very rarely say that anything is &lt;i&gt;impossible&lt;/i&gt;...]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-359893">Noam G</a>.</p>
<p>@Noam &#8211; I am not aware of any way to achieve this. But I very rarely say that anything is <i>impossible</i>&#8230;</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Noam G		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-359893</link>

		<dc:creator><![CDATA[Noam G]]></dc:creator>
		<pubDate>Mon, 26 Oct 2015 23:17:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-359893</guid>

					<description><![CDATA[Hi Yair,
Do you have any idea how could I find all the warnings occurred since a certain point and on?
&lt;b&gt;lastwarn&lt;/b&gt; returns only the last warning, but let&#039;s say that I want to test some code, and I want to track ALL the warnings it had produced. 
Do you think it&#039;s possible?]]></description>
			<content:encoded><![CDATA[<p>Hi Yair,<br />
Do you have any idea how could I find all the warnings occurred since a certain point and on?<br />
<b>lastwarn</b> returns only the last warning, but let&#8217;s say that I want to test some code, and I want to track ALL the warnings it had produced.<br />
Do you think it&#8217;s possible?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Matlab warning(&#039;error&#039;) produces not enough arguments error - DexPage		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-353260</link>

		<dc:creator><![CDATA[Matlab warning(&#039;error&#039;) produces not enough arguments error - DexPage]]></dc:creator>
		<pubDate>Fri, 17 Jul 2015 22:07:07 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-353260</guid>

					<description><![CDATA[[...] additional input (a message identifier). It is used for trapping/catching warnings as errors. See this Undocumented Matlab post and this MathWorks Newsgroup [...]]]></description>
			<content:encoded><![CDATA[<p>[&#8230;] additional input (a message identifier). It is used for trapping/catching warnings as errors. See this Undocumented Matlab post and this MathWorks Newsgroup [&#8230;]</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-214952</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 19 Jun 2013 19:16:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-214952</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-214877&quot;&gt;Steve Coleman&lt;/a&gt;.

@Steve - thanks for the clarification, duly noted.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-214877">Steve Coleman</a>.</p>
<p>@Steve &#8211; thanks for the clarification, duly noted.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Steve Coleman		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-214877</link>

		<dc:creator><![CDATA[Steve Coleman]]></dc:creator>
		<pubDate>Wed, 19 Jun 2013 15:37:10 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-214877</guid>

					<description><![CDATA[Just a note. The Try/Catch construct can have a tremendous performance hit if used inside a loop that is executed many many times, but this can very by environment. I have one report that would run fine on my laptop in a matter of hours, but when placed on the Big-Server in the basement that time literally turned into days. Profiling on each machine showed a very clear difference in the time allocated to the same try/catch, running on the same dataset, in the two different environments. After removing the try/catch statement from a very tight loop in the core of the main algorithm, that change made all the difference. 
 
Try/Catch by its very nature does a *lot* of housekeeping under the covers, to build stack frames that can be unwound back up to higher levels, which the if/else construct does not have to  do. If a condition is a simple test (e.g. &#062;0 or isempty) and in a very tight loop, it can be much more efficient to just use the if/else construct.  

After the above experience I now reserve try/catch for main-level to second-tier level functions and try to keep them out of the often called modules. Try/catch is great, but use them wisely. If its called more than 100 times in a given run then if/else may be a better bet. For checking user input or possibly tainted network data, try/catch is great! Using it in a loop running a million times, maybe not so much.]]></description>
			<content:encoded><![CDATA[<p>Just a note. The Try/Catch construct can have a tremendous performance hit if used inside a loop that is executed many many times, but this can very by environment. I have one report that would run fine on my laptop in a matter of hours, but when placed on the Big-Server in the basement that time literally turned into days. Profiling on each machine showed a very clear difference in the time allocated to the same try/catch, running on the same dataset, in the two different environments. After removing the try/catch statement from a very tight loop in the core of the main algorithm, that change made all the difference. </p>
<p>Try/Catch by its very nature does a *lot* of housekeeping under the covers, to build stack frames that can be unwound back up to higher levels, which the if/else construct does not have to  do. If a condition is a simple test (e.g. &gt;0 or isempty) and in a very tight loop, it can be much more efficient to just use the if/else construct.  </p>
<p>After the above experience I now reserve try/catch for main-level to second-tier level functions and try to keep them out of the often called modules. Try/catch is great, but use them wisely. If its called more than 100 times in a given run then if/else may be a better bet. For checking user input or possibly tainted network data, try/catch is great! Using it in a loop running a million times, maybe not so much.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Baz		</title>
		<link>https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-121109</link>

		<dc:creator><![CDATA[Baz]]></dc:creator>
		<pubDate>Mon, 12 Nov 2012 19:51:30 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3008#comment-121109</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-121105&quot;&gt;Yair Altman&lt;/a&gt;.

Thanks, the reason I am doing the coding this way is that rdivide is generating these error messages when it enounters a singular/badly conditioned matrix, so rdivide is clearly already using rcond() to test the conditioning of the matrices. I need to check the conditioning of the matrices, rather than me having to call rcond() again to duplicate this work, I would just like to get access to the rcond values and/or the decision as to whether the matrix is badly conditioned or not that rdivide is already producing. Ideally within a parallel structure if this is possible?

To be clear the parallel toolbox does still produce these warning messages, its just when I add:
&lt;pre lang=&#039;matlab&#039;&gt;
warnState(1) = warning(&#039;error&#039;, &#039;MATLAB:singularMatrix&#039;); 
warnState(2) = warning(&#039;error&#039;, &#039;MATLAB:illConditionedMatrix&#039;); 
&lt;/pre&gt;

to turn them into errors that the divergence between parfor and for arises.

Would I be better to just call rcond() myself, this will mean duplication of work (which is annoying), but as least it will work.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/trapping-warnings-efficiently#comment-121105">Yair Altman</a>.</p>
<p>Thanks, the reason I am doing the coding this way is that rdivide is generating these error messages when it enounters a singular/badly conditioned matrix, so rdivide is clearly already using rcond() to test the conditioning of the matrices. I need to check the conditioning of the matrices, rather than me having to call rcond() again to duplicate this work, I would just like to get access to the rcond values and/or the decision as to whether the matrix is badly conditioned or not that rdivide is already producing. Ideally within a parallel structure if this is possible?</p>
<p>To be clear the parallel toolbox does still produce these warning messages, its just when I add:</p>
<pre lang='matlab'>
warnState(1) = warning('error', 'MATLAB:singularMatrix'); 
warnState(2) = warning('error', 'MATLAB:illConditionedMatrix'); 
</pre>
<p>to turn them into errors that the divergence between parfor and for arises.</p>
<p>Would I be better to just call rcond() myself, this will mean duplication of work (which is annoying), but as least it will work.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
