<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931</link>
    <title>MATLAB Central Newsreader - Using functions and structures sensibly</title>
    <description>Feed for thread: Using functions and structures sensibly</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by 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>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 08 May 2008 13:10:20 -0400</pubDate>
      <title>Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431034</link>
      <author>Pete </author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I have some code that works, but I want to check whether I'm doing &lt;br&gt;
something horribly inefficient or unwise based upon how MATLAB deals with &lt;br&gt;
function arguments.&lt;br&gt;
&lt;br&gt;
Basically, I have a (reasonably) large structure, which has been preallocated &lt;br&gt;
with all the correct fields.&lt;br&gt;
I want to pass it to some functions (it isn't certain in advance which functions) &lt;br&gt;
that will modify some fields of the structure.&lt;br&gt;
&lt;br&gt;
A dramatically shortened version of the code might be:&lt;br&gt;
&lt;br&gt;
% preallocate structure&lt;br&gt;
S = struct( [field names and initial values] );&lt;br&gt;
% apply functions&lt;br&gt;
for ii = 1:numel(fh)&lt;br&gt;
	S = feval(fh{ii}, S);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
where fh is a cell array containing the names of certain functions stored in M &lt;br&gt;
files which begin something like,&lt;br&gt;
&lt;br&gt;
function myStruct = myFunction(myStruct)&lt;br&gt;
&lt;br&gt;
I read something that suggested modifying input arguments in MATLAB &lt;br&gt;
functions is bad most of the time.  I also wasn't sure about using the same &lt;br&gt;
variable name for both the input and the output, but in my mind this seems &lt;br&gt;
sensible for what I want to do.&lt;br&gt;
Is it sensible, or will it result in unnecessary overhead?  Would others here do &lt;br&gt;
things the same way or differently?&lt;br&gt;
&lt;br&gt;
Currently my code seems to work well and fast enough, but if it is bad &lt;br&gt;
practice and there is a better way then I'd like to learn it.  I'm using 2007b at &lt;br&gt;
the moment, and the code is part of a gui created using GUIDE.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Pete</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 15:07:07 -0400</pubDate>
      <title>Re: Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431066</link>
      <author>Peter Boettcher</author>
      <description>&quot;Pete &quot; &amp;lt;pete.dot.bankhead@btinternet.dot.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I have some code that works, but I want to check whether I'm doing &lt;br&gt;
&amp;gt; something horribly inefficient or unwise based upon how MATLAB deals with &lt;br&gt;
&amp;gt; function arguments.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Basically, I have a (reasonably) large structure, which has been preallocated &lt;br&gt;
&amp;gt; with all the correct fields.&lt;br&gt;
&amp;gt; I want to pass it to some functions (it isn't certain in advance which functions) &lt;br&gt;
&amp;gt; that will modify some fields of the structure.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; A dramatically shortened version of the code might be:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; % preallocate structure&lt;br&gt;
&amp;gt; S = struct( [field names and initial values] );&lt;br&gt;
&amp;gt; % apply functions&lt;br&gt;
&amp;gt; for ii = 1:numel(fh)&lt;br&gt;
&amp;gt; 	S = feval(fh{ii}, S);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; where fh is a cell array containing the names of certain functions stored in M &lt;br&gt;
&amp;gt; files which begin something like,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function myStruct = myFunction(myStruct)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I read something that suggested modifying input arguments in MATLAB &lt;br&gt;
&amp;gt; functions is bad most of the time.  I also wasn't sure about using the same &lt;br&gt;
&amp;gt; variable name for both the input and the output, but in my mind this seems &lt;br&gt;
&amp;gt; sensible for what I want to do.&lt;br&gt;
&amp;gt; Is it sensible, or will it result in unnecessary overhead?  Would others here do &lt;br&gt;
&amp;gt; things the same way or differently?&lt;br&gt;
&lt;br&gt;
Yes, that's sensible.  For purposes of the copy-on-write behavior,&lt;br&gt;
fields of structures (and elements of cell arrays) behave the same as&lt;br&gt;
independent variables.  That is, if you modify one field of S, only the&lt;br&gt;
struct header (~100-200 bytes) and the modified field are copied.&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 15:21:04 -0400</pubDate>
      <title>Re: Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431069</link>
      <author>Pete </author>
      <description>Excellent, that's good to know - thanks!</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 15:30:55 -0400</pubDate>
      <title>Re: Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431074</link>
      <author>someone </author>
      <description>&quot;Pete &quot; &amp;lt;pete.dot.bankhead@btinternet.dot.com&amp;gt; wrote in &lt;br&gt;
message &amp;lt;fvuu3s$bd$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have some code that works, but I want to check whether &lt;br&gt;
I'm doing &lt;br&gt;
&amp;gt; something horribly inefficient or unwise based upon how &lt;br&gt;
MATLAB deals with &lt;br&gt;
&amp;gt; function arguments.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Basically, I have a (reasonably) large structure, which &lt;br&gt;
has been preallocated &lt;br&gt;
&amp;gt; with all the correct fields.&lt;br&gt;
&amp;gt; I want to pass it to some functions (it isn't certain in &lt;br&gt;
advance which functions) &lt;br&gt;
&amp;gt; that will modify some fields of the structure.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A dramatically shortened version of the code might be:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % preallocate structure&lt;br&gt;
&amp;gt; S = struct( [field names and initial values] );&lt;br&gt;
&amp;gt; % apply functions&lt;br&gt;
&amp;gt; for ii = 1:numel(fh)&lt;br&gt;
&amp;gt; 	S = feval(fh{ii}, S);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; where fh is a cell array containing the names of certain &lt;br&gt;
functions stored in M &lt;br&gt;
&amp;gt; files which begin something like,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function myStruct = myFunction(myStruct)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I read something that suggested modifying input arguments &lt;br&gt;
in MATLAB &lt;br&gt;
&amp;gt; functions is bad most of the time.&lt;br&gt;
&lt;br&gt;
The only reason why I know modifying inputs &quot;is bad&quot; is &lt;br&gt;
that MATLAB will pass the inputs by value (instead of by &lt;br&gt;
reference).  But, one way or another, you are creating &lt;br&gt;
another copy of myStruct anyway.&lt;br&gt;
&lt;br&gt;
&amp;gt;  I also wasn't sure about using the same &lt;br&gt;
&amp;gt; variable name for both the input and the output, but in &lt;br&gt;
my mind this seems &lt;br&gt;
&amp;gt; sensible for what I want to do.&lt;br&gt;
&lt;br&gt;
You could use something like:&lt;br&gt;
&lt;br&gt;
function myStruct = myFunction(myInpStruct)&lt;br&gt;
myStruct = myInpStruct;&lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
and call it exactly the same way:&lt;br&gt;
&lt;br&gt;
myStruct = myFunction(myStruct)&lt;br&gt;
&lt;br&gt;
But, I don't know that this is any more efficient.  (Might &lt;br&gt;
make it easier to debug MyFunction.)&lt;br&gt;
&lt;br&gt;
&amp;gt; Is it sensible, or will it result in unnecessary &lt;br&gt;
overhead?  Would others here do &lt;br&gt;
&amp;gt; things the same way or differently?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Currently my code seems to work well and fast enough, but &lt;br&gt;
if it is bad &lt;br&gt;
&amp;gt; practice and there is a better way then I'd like to learn &lt;br&gt;
it.  I'm using 2007b at &lt;br&gt;
&amp;gt; the moment, and the code is part of a gui created using &lt;br&gt;
GUIDE.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Pete</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 16:13:14 -0400</pubDate>
      <title>Re: Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431084</link>
      <author>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)</author>
      <description>In article &amp;lt;fvv6bf$4gd$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
someone  &amp;lt;someone@somewhere.net&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;The only reason why I know modifying inputs &quot;is bad&quot; is &lt;br&gt;
&amp;gt;that MATLAB will pass the inputs by value (instead of by &lt;br&gt;
&amp;gt;reference).  But, one way or another, you are creating &lt;br&gt;
&amp;gt;another copy of myStruct anyway.&lt;br&gt;
&lt;br&gt;
I -gather- (perhaps incorrectly) from other postings that the&lt;br&gt;
special case of having the same variable on input and output&lt;br&gt;
of a function is handled by modifying the variable in-place&lt;br&gt;
rather than creating a copy of the variable (other than&lt;br&gt;
if the variable happens to share data with another variable.)&lt;br&gt;
-- &lt;br&gt;
&amp;nbsp;&amp;nbsp;&quot;Man's life is but a jest,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;A dream, a shadow, bubble, air, a vapor at the best.&quot;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-- George Walter Thornbury</description>
    </item>
    <item>
      <pubDate>Thu, 08 May 2008 21:31:01 -0400</pubDate>
      <title>Re: Using functions and structures sensibly</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168931#431155</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;fvv8qq$1d8$1@canopus.cc.umanitoba.ca&amp;gt;, roberson@ibd.nrc-&lt;br&gt;
cnrc.gc.ca says...&lt;br&gt;
&amp;gt; In article &amp;lt;fvv6bf$4gd$1@fred.mathworks.com&amp;gt;,&lt;br&gt;
&amp;gt; someone  &amp;lt;someone@somewhere.net&amp;gt; wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;The only reason why I know modifying inputs &quot;is bad&quot; is &lt;br&gt;
&amp;gt; &amp;gt;that MATLAB will pass the inputs by value (instead of by &lt;br&gt;
&amp;gt; &amp;gt;reference).  But, one way or another, you are creating &lt;br&gt;
&amp;gt; &amp;gt;another copy of myStruct anyway.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I -gather- (perhaps incorrectly) from other postings that the&lt;br&gt;
&amp;gt; special case of having the same variable on input and output&lt;br&gt;
&amp;gt; of a function is handled by modifying the variable in-place&lt;br&gt;
&amp;gt; rather than creating a copy of the variable (other than&lt;br&gt;
&amp;gt; if the variable happens to share data with another variable.)&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Sometimes...  depends on how it's called from the caller (same right and &lt;br&gt;
left hand side name required) AND requires the calculation to be do-able &lt;br&gt;
inplace.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren/&quot;&gt;http://blogs.mathworks.com/loren/&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

