<?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: MATLAB Programming Practices</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/</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, 16 Feb 2012 04:40:26 +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/2006/04/12/matlab-programming-practices/#comment-31414</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 11 Jun 2010 17:14:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-31414</guid>
		<description>Ian-

You would probably be better off using a plain mex-file rather than emlmex - which is meant for working with Simulink.  There is a lot of information in the External Interfaces section of the MATLAB documentation for more information : http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html 

You *can* sometimes get substantial speedups, but you often can do close to as well using &quot;well-written&quot; MATLAB code too.  That&#039;s what I tend to focus on in this blog.

--Loren</description>
		<content:encoded><![CDATA[<p>Ian-</p>
<p>You would probably be better off using a plain mex-file rather than emlmex &#8211; which is meant for working with Simulink.  There is a lot of information in the External Interfaces section of the MATLAB documentation for more information : <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html" rel="nofollow">http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html</a> </p>
<p>You *can* sometimes get substantial speedups, but you often can do close to as well using &#8220;well-written&#8221; MATLAB code too.  That&#8217;s what I tend to focus on in this blog.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian Dew-Becker</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-31413</link>
		<dc:creator>Ian Dew-Becker</dc:creator>
		<pubDate>Fri, 11 Jun 2010 16:59:38 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-31413</guid>
		<description>Hi Loren,

I searched your blog and couldn&#039;t find much with relevance to mex files.

I don&#039;t really know how to code in C, but I stumbled across emlmex recently (when digging into the guts of polyval to try to speed it up).  I wrote a simple and ugly for loop (loop over a vector and output the square of the sine of each element) and ran it as an m-file and then using emlmex to generate a mex file.  The mex-file sped my code up by a factor of 2.

I&#039;m currently working on an economics problem, used emlmex on an m-file I had, and it cut the execution time by 60%.

When I google emlmex, though, I find almost nothing on the internet about it.  I&#039;ve been using matlab for a few years, and have read a lot of different documents on various speedups.  I&#039;ve never seen emlmex mentioned, though.  Do you know why this is?

I&#039;m commenting here because it seems like it might be useful for other people to be able to get substantial speedups on things like loops without much effort.

For your reference, here&#039;s the .m file I used:

-----------------------------
function tester=tester(param,data)

%%%%%%%%%%%%
b=data;
for i=1:50000
    b(i)=param*sin(b(i)^2);
end

tester=sum(b);
--------------------------------------
I ran it on a 50000x1 vector and timed it by running it 200 times with the m- and mex-files</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I searched your blog and couldn&#8217;t find much with relevance to mex files.</p>
<p>I don&#8217;t really know how to code in C, but I stumbled across emlmex recently (when digging into the guts of polyval to try to speed it up).  I wrote a simple and ugly for loop (loop over a vector and output the square of the sine of each element) and ran it as an m-file and then using emlmex to generate a mex file.  The mex-file sped my code up by a factor of 2.</p>
<p>I&#8217;m currently working on an economics problem, used emlmex on an m-file I had, and it cut the execution time by 60%.</p>
<p>When I google emlmex, though, I find almost nothing on the internet about it.  I&#8217;ve been using matlab for a few years, and have read a lot of different documents on various speedups.  I&#8217;ve never seen emlmex mentioned, though.  Do you know why this is?</p>
<p>I&#8217;m commenting here because it seems like it might be useful for other people to be able to get substantial speedups on things like loops without much effort.</p>
<p>For your reference, here&#8217;s the .m file I used:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
function tester=tester(param,data)</p>
<p>%%%%%%%%%%%%<br />
b=data;<br />
for i=1:50000<br />
    b(i)=param*sin(b(i)^2);<br />
end</p>
<p>tester=sum(b);<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
I ran it on a 50000&#215;1 vector and timed it by running it 200 times with the m- and mex-files</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-29567</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 04 Jul 2008 10:39:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-29567</guid>
		<description>Oktay-

I recommend you look at M-files we ship with MATLAB that seem similar in the idea of the extra inputs and see how the error checking is done there.  One way that&#039;s newer is to use an inputParser object for at least part of the work. Another tool is the function assert.

--Loren</description>
		<content:encoded><![CDATA[<p>Oktay-</p>
<p>I recommend you look at M-files we ship with MATLAB that seem similar in the idea of the extra inputs and see how the error checking is done there.  One way that&#8217;s newer is to use an inputParser object for at least part of the work. Another tool is the function assert.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oktay</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-29566</link>
		<dc:creator>Oktay</dc:creator>
		<pubDate>Fri, 04 Jul 2008 07:16:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-29566</guid>
		<description>Hello!

I have a function I wrote that is very similiar to FREAD in terms of input arguements:

A = FREAD(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT)

Here, SIZE and SKIP arguements are optional that is the function shows an intelligent behaviour when we type

a = fread(fid,precision,machineformat)

My function also has these optional arguements depending on the type of inputs. So, can you suggest basic methods/guidelines for distributing the input arguments properly, and a robust error checking inside the m-file?

Thank you!

Oktay</description>
		<content:encoded><![CDATA[<p>Hello!</p>
<p>I have a function I wrote that is very similiar to FREAD in terms of input arguements:</p>
<p>A = FREAD(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT)</p>
<p>Here, SIZE and SKIP arguements are optional that is the function shows an intelligent behaviour when we type</p>
<p>a = fread(fid,precision,machineformat)</p>
<p>My function also has these optional arguements depending on the type of inputs. So, can you suggest basic methods/guidelines for distributing the input arguments properly, and a robust error checking inside the m-file?</p>
<p>Thank you!</p>
<p>Oktay</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-16216</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 11 May 2007 10:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-16216</guid>
		<description>Please see this message from Steve&#039;s blog: http://blogs.mathworks.com/steve/category/blog-policies/</description>
		<content:encoded><![CDATA[<p>Please see this message from Steve&#8217;s blog: <a href="http://blogs.mathworks.com/steve/category/blog-policies/" rel="nofollow">http://blogs.mathworks.com/steve/category/blog-policies/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sana</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-16215</link>
		<dc:creator>sana</dc:creator>
		<pubDate>Fri, 11 May 2007 06:57:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-16215</guid>
		<description>i want some help in numerical analysis,if u can help me out with the Bessel&#039;s central diffrence formula using MATLAB</description>
		<content:encoded><![CDATA[<p>i want some help in numerical analysis,if u can help me out with the Bessel&#8217;s central diffrence formula using MATLAB</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad Phelan</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-145</link>
		<dc:creator>Brad Phelan</dc:creator>
		<pubDate>Wed, 19 Apr 2006 19:57:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-145</guid>
		<description>Test driven development is one key to good software development. Write tests early and often and less problems arrive later. When coding in C under Linux/LynxOS I use &quot;Check&quot; 

http://check.sourceforge.net. 

When coding under Matlab I found no decent testing framework so I wrote my own. 

One of the things I find about writing tests is not only do they provide regression error protection but they help me understand my own code and designs. I may come back to some obscure corner of an API I have written after a month and forget exactly how I intended it to be used. If I have a test case for it then more often than not it makes clear to me what I was up to.


http://xtargets.com/cms/Tutorials/Matlab-Programming/MUnit-Matlab-Unit-Testing.html

which you can download and use. It&#039;s simple to get started with and integrates into the Matlab web browser for report generation and interactive testing.</description>
		<content:encoded><![CDATA[<p>Test driven development is one key to good software development. Write tests early and often and less problems arrive later. When coding in C under Linux/LynxOS I use &#8220;Check&#8221; </p>
<p><a href="http://check.sourceforge.net" rel="nofollow">http://check.sourceforge.net</a>. </p>
<p>When coding under Matlab I found no decent testing framework so I wrote my own. </p>
<p>One of the things I find about writing tests is not only do they provide regression error protection but they help me understand my own code and designs. I may come back to some obscure corner of an API I have written after a month and forget exactly how I intended it to be used. If I have a test case for it then more often than not it makes clear to me what I was up to.</p>
<p><a href="http://xtargets.com/cms/Tutorials/Matlab-Programming/MUnit-Matlab-Unit-Testing.html" rel="nofollow">http://xtargets.com/cms/Tutorials/Matlab-Programming/MUnit-Matlab-Unit-Testing.html</a></p>
<p>which you can download and use. It&#8217;s simple to get started with and integrates into the Matlab web browser for report generation and interactive testing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Felloweskraft</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-142</link>
		<dc:creator>Felloweskraft</dc:creator>
		<pubDate>Thu, 13 Apr 2006 02:10:12 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-142</guid>
		<description>This may be obvious, but I&#039;ve always found it easiest to work in dimensionless units for as much of a program as possible.</description>
		<content:encoded><![CDATA[<p>This may be obvious, but I&#8217;ve always found it easiest to work in dimensionless units for as much of a program as possible.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-141</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 12 Apr 2006 19:59:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-141</guid>
		<description>&lt;p&gt;
I think there is sometimes a gray area, actually.  But there is a real difference in some cases.  Here&#039;s a couple of examples.
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Extend a function that works correctly for &quot;small&quot; input to the rest of the real line.&lt;/b&gt;  What is &quot;small&quot;? Do you check for it and error if the input doesn&#039;t meet the criteria?  If so, you are okay.  If you think you might need it later for a different range, it&#039;s best to make that clear with documentation and an error, until you upgrade it.  In this case, e.g., bessel functions with large input, I would prefer to make the function work correctly from the start.  Then I can always rely on it.  To me, this fits more closely into &lt;i&gt;Make the code general and document ranges and exceptions.&lt;/i&gt;

&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Extend a function that works for real input onto the complex plane.&lt;/b&gt;  And spend lots of time figuring out how to do the analytic continuation and defining appropriate branch cuts so inputs can be anywhere in the complex plane.  Again, either error and document carefully, or implement.  Is it worth implementing?  Can you imagine using it that way, or is it just cool?  If you don&#039;t really see your way clear to a definite use case, even if you won&#039;t need it for that right away, it might not be worth the effort right now. To me, this one fits more closely into &lt;i&gt;Avoid speculative generality.&lt;/i&gt;
&lt;/p&gt;</description>
		<content:encoded><![CDATA[<p>
I think there is sometimes a gray area, actually.  But there is a real difference in some cases.  Here&#8217;s a couple of examples.
</p>
<p>
<b>Extend a function that works correctly for &#8220;small&#8221; input to the rest of the real line.</b>  What is &#8220;small&#8221;? Do you check for it and error if the input doesn&#8217;t meet the criteria?  If so, you are okay.  If you think you might need it later for a different range, it&#8217;s best to make that clear with documentation and an error, until you upgrade it.  In this case, e.g., bessel functions with large input, I would prefer to make the function work correctly from the start.  Then I can always rely on it.  To me, this fits more closely into <i>Make the code general and document ranges and exceptions.</i></p>
<p>
<b>Extend a function that works for real input onto the complex plane.</b>  And spend lots of time figuring out how to do the analytic continuation and defining appropriate branch cuts so inputs can be anywhere in the complex plane.  Again, either error and document carefully, or implement.  Is it worth implementing?  Can you imagine using it that way, or is it just cool?  If you don&#8217;t really see your way clear to a definite use case, even if you won&#8217;t need it for that right away, it might not be worth the effort right now. To me, this one fits more closely into <i>Avoid speculative generality.</i></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John O'Leary</title>
		<link>http://blogs.mathworks.com/loren/2006/04/12/matlab-programming-practices/#comment-140</link>
		<dc:creator>John O'Leary</dc:creator>
		<pubDate>Wed, 12 Apr 2006 19:35:01 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=33#comment-140</guid>
		<description>From your principles:

&lt;i&gt;Avoid speculative generality.&lt;/i&gt;

&lt;i&gt;Make the code general and document ranges and exceptions.&lt;/i&gt;

What is the difference between general code and &quot;speculative generality&quot;?  Is there a line between the two?</description>
		<content:encoded><![CDATA[<p>From your principles:</p>
<p><i>Avoid speculative generality.</i></p>
<p><i>Make the code general and document ranges and exceptions.</i></p>
<p>What is the difference between general code and &#8220;speculative generality&#8221;?  Is there a line between the two?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

