<?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: Constants</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/09/13/constants/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/09/13/constants/</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: Roger Boyer</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-30422</link>
		<dc:creator>Roger Boyer</dc:creator>
		<pubDate>Mon, 29 Jun 2009 14:50:59 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-30422</guid>
		<description>I know this thread is old, but I would like to add my motivation for constants. I&#039;m writing m-File S-Functions and referencing Dwork arrays would be much clearer if there existed &quot;c&quot; style header files.  I&#039;m otherwise left with either creating a function (as described above) to define each index into the Dwork array, or using a verbose class/property technique (Constants.SFunc.WFM to demystify &#039;1&#039;).  A single function that returned a structure seems the best compromise, but I would prefer that Mathworks publish a coding style recommendation that could be more uniformly adopted (ideally with a terse format).

Thanks!</description>
		<content:encoded><![CDATA[<p>I know this thread is old, but I would like to add my motivation for constants. I&#8217;m writing m-File S-Functions and referencing Dwork arrays would be much clearer if there existed &#8220;c&#8221; style header files.  I&#8217;m otherwise left with either creating a function (as described above) to define each index into the Dwork array, or using a verbose class/property technique (Constants.SFunc.WFM to demystify &#8217;1&#8242;).  A single function that returned a structure seems the best compromise, but I would prefer that Mathworks publish a coding style recommendation that could be more uniformly adopted (ideally with a terse format).</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gene Saxon</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-30060</link>
		<dc:creator>Gene Saxon</dc:creator>
		<pubDate>Wed, 25 Feb 2009 20:36:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-30060</guid>
		<description>I wanted to define a large number of constants in a GUI that I wrote and found that I could make use of anonymous functions as a method of &#039;protecting&#039; them from accidental change.

The initialization code in my GUI follows the following sequence:

c = CreateConstants();   % Returns a structure, c, of &#039;constants&#039;
h = CreateGuiObjects(c); % Returns a structure, h, of handles to GUI objects.
AssignGuiCallbacks(h,c); % Assigns callbacks to objects

As long as the assignments made in AssignGuiCallbacks use anonymous function calls, the argument c is fixed at the time the assignment is made (refer to Matlab help on the topic). An example of an assignment might be:
 
set(h.tmrMain,&#039;TimerFcn&#039;,{@tmrMain_TimerFcn,h,c});

Whenever the TimerFcn event is subsequently triggered, tmrMain_TimerFcn will always use the value of c that was defined at the time the anonymous function call was defined.

I have found this technique to be very useful in this application as all functions are callbacks, or called as a sub-routine from a function that is a callback.

This method is still not bulletproof; there is nothing to prevent one from changing c from withing a function, although changes will not persist between calls.

I also find that using a structure such as this mitigates the risk of changes as a) it is obvious that anything that is part of the structure is not meant to be changed and b) a short name like &#039;c&#039; reserves plenty of room for descriptive field names. I prefer to name globals in ALL CAPS in order to make the distinction between the two variable types. 

Having said all this, I _would_ like the ability to define true constants in Matlab. My concern about the &#039;function&#039; method outlined by Loren above, is the potential performance hit encountered if many constants are used in time-sensitive calculations. Is this a reasonable concern?

Gene</description>
		<content:encoded><![CDATA[<p>I wanted to define a large number of constants in a GUI that I wrote and found that I could make use of anonymous functions as a method of &#8216;protecting&#8217; them from accidental change.</p>
<p>The initialization code in my GUI follows the following sequence:</p>
<p>c = CreateConstants();   % Returns a structure, c, of &#8216;constants&#8217;<br />
h = CreateGuiObjects(c); % Returns a structure, h, of handles to GUI objects.<br />
AssignGuiCallbacks(h,c); % Assigns callbacks to objects</p>
<p>As long as the assignments made in AssignGuiCallbacks use anonymous function calls, the argument c is fixed at the time the assignment is made (refer to Matlab help on the topic). An example of an assignment might be:</p>
<p>set(h.tmrMain,&#8217;TimerFcn&#8217;,{@tmrMain_TimerFcn,h,c});</p>
<p>Whenever the TimerFcn event is subsequently triggered, tmrMain_TimerFcn will always use the value of c that was defined at the time the anonymous function call was defined.</p>
<p>I have found this technique to be very useful in this application as all functions are callbacks, or called as a sub-routine from a function that is a callback.</p>
<p>This method is still not bulletproof; there is nothing to prevent one from changing c from withing a function, although changes will not persist between calls.</p>
<p>I also find that using a structure such as this mitigates the risk of changes as a) it is obvious that anything that is part of the structure is not meant to be changed and b) a short name like &#8216;c&#8217; reserves plenty of room for descriptive field names. I prefer to name globals in ALL CAPS in order to make the distinction between the two variable types. </p>
<p>Having said all this, I _would_ like the ability to define true constants in Matlab. My concern about the &#8216;function&#8217; method outlined by Loren above, is the potential performance hit encountered if many constants are used in time-sensitive calculations. Is this a reasonable concern?</p>
<p>Gene</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: StephenLL</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-27271</link>
		<dc:creator>StephenLL</dc:creator>
		<pubDate>Thu, 06 Mar 2008 22:19:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-27271</guid>
		<description>Check out the new class system in R2008a for support for constants.</description>
		<content:encoded><![CDATA[<p>Check out the new class system in R2008a for support for constants.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Greg Holley</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-27204</link>
		<dc:creator>Greg Holley</dc:creator>
		<pubDate>Thu, 06 Mar 2008 04:14:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-27204</guid>
		<description>The trick about not being able to overwrite a function once you&#039;ve called it is a useful one. I imagine I could declare constants via some mechanism like:

    % Constants - call f&#039;n here to prevent overwriting
    PLANCK;
    PI_OVER_TWO;

The problem is, this seems to be a very clunky way to provide constant-like functionality without having to add them to the Matlab scripting language. Why not just figure out how to support constants, and then support them?</description>
		<content:encoded><![CDATA[<p>The trick about not being able to overwrite a function once you&#8217;ve called it is a useful one. I imagine I could declare constants via some mechanism like:</p>
<p>    % Constants &#8211; call f&#8217;n here to prevent overwriting<br />
    PLANCK;<br />
    PI_OVER_TWO;</p>
<p>The problem is, this seems to be a very clunky way to provide constant-like functionality without having to add them to the Matlab scripting language. Why not just figure out how to support constants, and then support them?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Lotz</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-16170</link>
		<dc:creator>Joe Lotz</dc:creator>
		<pubDate>Mon, 02 Apr 2007 20:17:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-16170</guid>
		<description>Not to spam this comment board, but a quick search in FEX turned up this 
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13622&amp;objectType=file

It is an iteresting script that allows you to define constants What it does is automatically create functions like the technique suggested by Loren such that you get an error if you try to use a variable of the same name.

I&#039;ve only briefly looked at the code so cannot comment to its limitations but it looks useful.</description>
		<content:encoded><![CDATA[<p>Not to spam this comment board, but a quick search in FEX turned up this<br />
<a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13622&#038;objectType=file" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=13622&#038;objectType=file</a></p>
<p>It is an iteresting script that allows you to define constants What it does is automatically create functions like the technique suggested by Loren such that you get an error if you try to use a variable of the same name.</p>
<p>I&#8217;ve only briefly looked at the code so cannot comment to its limitations but it looks useful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Lotz</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-16169</link>
		<dc:creator>Joe Lotz</dc:creator>
		<pubDate>Mon, 02 Apr 2007 20:09:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-16169</guid>
		<description>Have have used the .m file include like the previous posters suggested, however, I agree with Steve about the overhead. I have done some testing and found that if you save your constants to a .mat data file and then call that instead of a script the overhead is reduced. 

Once again, this does not prevent overwriting as does the function definition technique. I agree and wish you could define constants and &quot;lock&quot; them.</description>
		<content:encoded><![CDATA[<p>Have have used the .m file include like the previous posters suggested, however, I agree with Steve about the overhead. I have done some testing and found that if you save your constants to a .mat data file and then call that instead of a script the overhead is reduced. </p>
<p>Once again, this does not prevent overwriting as does the function definition technique. I agree and wish you could define constants and &#8220;lock&#8221; them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-16114</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 08 Mar 2007 04:03:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-16114</guid>
		<description>How I have handled this in the past was to create a script will all the constants I cared about.  I usually named it ConstantsInclude because I really wish the C-like functionality was in matlab.  I then called the script at the top of every function I created. The advantage of this was that I only had to define constants in one file.  The problems are
1) The constants weren&#039;t protected within a function (I didn&#039;t really care too much about this), and 
2) The overhead was huge.  For mainy functions, the include script (which defined ~200 constants) took longer than the function. (This was the part that really bugged me)

The overhead is less if all the constants were is a structure (i.e. Constant.PI, Constant.TWOPI), but that makes the code a little gross.

I would be much happier (and swear a little less at Matlab) if you could define constants like in C, that would be handled before runtime.</description>
		<content:encoded><![CDATA[<p>How I have handled this in the past was to create a script will all the constants I cared about.  I usually named it ConstantsInclude because I really wish the C-like functionality was in matlab.  I then called the script at the top of every function I created. The advantage of this was that I only had to define constants in one file.  The problems are<br />
1) The constants weren&#8217;t protected within a function (I didn&#8217;t really care too much about this), and<br />
2) The overhead was huge.  For mainy functions, the include script (which defined ~200 constants) took longer than the function. (This was the part that really bugged me)</p>
<p>The overhead is less if all the constants were is a structure (i.e. Constant.PI, Constant.TWOPI), but that makes the code a little gross.</p>
<p>I would be much happier (and swear a little less at Matlab) if you could define constants like in C, that would be handled before runtime.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raul Rato</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-15190</link>
		<dc:creator>Raul Rato</dc:creator>
		<pubDate>Thu, 23 Nov 2006 16:50:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-15190</guid>
		<description>For clarity, I&#039;m used to define CONSTANTS in my code, instead of &quot;magic buried numbers&quot;.

I like simple solutions.
I&#039;d like to see a keyword, mconstant, for example, that
allow the folowing construct:

mconstant TWO__PI +6.28;

---</description>
		<content:encoded><![CDATA[<p>For clarity, I&#8217;m used to define CONSTANTS in my code, instead of &#8220;magic buried numbers&#8221;.</p>
<p>I like simple solutions.<br />
I&#8217;d like to see a keyword, mconstant, for example, that<br />
allow the folowing construct:</p>
<p>mconstant TWO__PI +6.28;</p>
<p>&#8212;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Wild</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-13770</link>
		<dc:creator>Michael Wild</dc:creator>
		<pubDate>Mon, 23 Oct 2006 08:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-13770</guid>
		<description>What I&#039;d like to see is some features allowing to

a) restrict the data type (and array shape/size?) on function arguments. Testing with IS*, NDIMS and SIZE sometimes becomes very tedious.

b) specify that a function argument is intended to be CONST. A syntax like

function fun( const a )

would be useful.

c) override the copy-on-write-semantics, meaning that I want to modify the value of the argument itself, not a copy. If I want to have a function which changes a certain variable, currently I have to do convolutions such as
a = fun(a)
which, is bound to be inefficient. However, I don&#039;t think it would be a good idea to introduce &quot;real&quot; reference parameters (such as C/c++ do), as this would change the semantics of Matlab programs too much. Perhaps a syntax could be devised to force the user to use the same name for input and output and then give Matlab a cue to handle the argument as a reference and not apply copy-on-write. A syntax like this might do it:

function [a,b,c] = fun( ref a, b, c )

where only &quot;a&quot; would be protected from copy-on-write.</description>
		<content:encoded><![CDATA[<p>What I&#8217;d like to see is some features allowing to</p>
<p>a) restrict the data type (and array shape/size?) on function arguments. Testing with IS*, NDIMS and SIZE sometimes becomes very tedious.</p>
<p>b) specify that a function argument is intended to be CONST. A syntax like</p>
<p>function fun( const a )</p>
<p>would be useful.</p>
<p>c) override the copy-on-write-semantics, meaning that I want to modify the value of the argument itself, not a copy. If I want to have a function which changes a certain variable, currently I have to do convolutions such as<br />
a = fun(a)<br />
which, is bound to be inefficient. However, I don&#8217;t think it would be a good idea to introduce &#8220;real&#8221; reference parameters (such as C/c++ do), as this would change the semantics of Matlab programs too much. Perhaps a syntax could be devised to force the user to use the same name for input and output and then give Matlab a cue to handle the argument as a reference and not apply copy-on-write. A syntax like this might do it:</p>
<p>function [a,b,c] = fun( ref a, b, c )</p>
<p>where only &#8220;a&#8221; would be protected from copy-on-write.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wmar</title>
		<link>http://blogs.mathworks.com/loren/2006/09/13/constants/#comment-12024</link>
		<dc:creator>wmar</dc:creator>
		<pubDate>Thu, 14 Sep 2006 22:50:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=54#comment-12024</guid>
		<description>I do a lot of physcial science calculations, so I have a lot of constants for mass of electron, hbar, Boltzmann&#039;s constant, etc.  To get around typing it each time, I call an m-file script called fundamentalconstants.m to declare those variables every time I need fundamental constants.  Persistent variables are interesting, hopefully that&#039;ll protect me from overriding the value.  I never really had a problem with overriding them though.</description>
		<content:encoded><![CDATA[<p>I do a lot of physcial science calculations, so I have a lot of constants for mass of electron, hbar, Boltzmann&#8217;s constant, etc.  To get around typing it each time, I call an m-file script called fundamentalconstants.m to declare those variables every time I need fundamental constants.  Persistent variables are interesting, hopefully that&#8217;ll protect me from overriding the value.  I never really had a problem with overriding them though.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

