<?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: Command and Function Syntaxes in MATLAB</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/</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/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-32542</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 12 Oct 2011 11:20:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-32542</guid>
		<description>Roshan-

Either do

myfun abc3b

or

myfun(&#039;abc3b&#039;)

--Loren</description>
		<content:encoded><![CDATA[<p>Roshan-</p>
<p>Either do</p>
<p>myfun abc3b</p>
<p>or</p>
<p>myfun(&#8216;abc3b&#8217;)</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roshan</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-32541</link>
		<dc:creator>roshan</dc:creator>
		<pubDate>Wed, 12 Oct 2011 10:20:49 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-32541</guid>
		<description>Hi.I have a question. I don&#039;t know how to pass a string for example &#039;abc3b&#039; into my function. I am defining a function that gets such a string as an input and I don&#039;t know how to do it.

Thanx</description>
		<content:encoded><![CDATA[<p>Hi.I have a question. I don&#8217;t know how to pass a string for example &#8216;abc3b&#8217; into my function. I am defining a function that gets such a string as an input and I don&#8217;t know how to do it.</p>
<p>Thanx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-18437</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 28 Sep 2007 11:22:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-18437</guid>
		<description>Pepi-

You need to read the documentation.  Perhaps x should be a sym.  Or else numeric values.

--Loren</description>
		<content:encoded><![CDATA[<p>Pepi-</p>
<p>You need to read the documentation.  Perhaps x should be a sym.  Or else numeric values.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pepi</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-18436</link>
		<dc:creator>Pepi</dc:creator>
		<pubDate>Fri, 28 Sep 2007 11:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-18436</guid>
		<description>&gt;&gt; sin(&#039;x&#039;)^2+cos(&#039;x&#039;)^2
??? Function &#039;sin&#039; is not defined for values of class &#039;char&#039;.</description>
		<content:encoded><![CDATA[<p>&gt;&gt; sin(&#8216;x&#8217;)^2+cos(&#8216;x&#8217;)^2<br />
??? Function &#8216;sin&#8217; is not defined for values of class &#8216;char&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Silas Leavesley</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-16288</link>
		<dc:creator>Silas Leavesley</dc:creator>
		<pubDate>Fri, 08 Jun 2007 20:17:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-16288</guid>
		<description>Thanks, Steve,

That seemed to work just like you said it would.  Now I just have to adjust my code so that everything converges in less than an hour!

-Silas</description>
		<content:encoded><![CDATA[<p>Thanks, Steve,</p>
<p>That seemed to work just like you said it would.  Now I just have to adjust my code so that everything converges in less than an hour!</p>
<p>-Silas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-16287</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Fri, 08 Jun 2007 13:36:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-16287</guid>
		<description>Silas,

How to do this depends on which version of MATLAB you&#039;re using.  If you&#039;re using MATLAB 7.0 (R14) or later, define the variables a, b, and c before the FMINCON call and use an anonymous function:

a = 1; b = 2; c = 3;
x1 = fmincon(@(x) myfun(a, b, c, x), ...)

You can use another anonymous function for funcon.

If you&#039;re using a version of MATLAB prior to 7.0 (R14), you&#039;ll need to do a little bit more work, since the input argument over which you&#039;re trying to optimize is not the first input.  The easiest way I can think of would be to write an inline function:

% Note the order of the inputs to the inline function.
% FMINCON optimizes over the first input of your objective
% function, so x needs to be the first input of the inline
% function.
%
% That&#039;s the reason the anonymous function in the example
% above has x as its only input argument.

f = inline(&#039;myfun(a, b, c, x)&#039;, &#039;x&#039;, &#039;a&#039;, &#039;b&#039;, &#039;c&#039;);


Call FMINCON using this inline function as the first input and specify a, b, and c as additional parameters in FMINCON, after the options structure (the P1, P2, etc. inputs in the M-file help in versions prior to R14) just as described in this document from the Technical Support site:

http://www.mathworks.com/support/solutions/data/1-19HM6.html?solution=1-19HM6</description>
		<content:encoded><![CDATA[<p>Silas,</p>
<p>How to do this depends on which version of MATLAB you&#8217;re using.  If you&#8217;re using MATLAB 7.0 (R14) or later, define the variables a, b, and c before the FMINCON call and use an anonymous function:</p>
<p>a = 1; b = 2; c = 3;<br />
x1 = fmincon(@(x) myfun(a, b, c, x), &#8230;)</p>
<p>You can use another anonymous function for funcon.</p>
<p>If you&#8217;re using a version of MATLAB prior to 7.0 (R14), you&#8217;ll need to do a little bit more work, since the input argument over which you&#8217;re trying to optimize is not the first input.  The easiest way I can think of would be to write an inline function:</p>
<p>% Note the order of the inputs to the inline function.<br />
% FMINCON optimizes over the first input of your objective<br />
% function, so x needs to be the first input of the inline<br />
% function.<br />
%<br />
% That&#8217;s the reason the anonymous function in the example<br />
% above has x as its only input argument.</p>
<p>f = inline(&#8216;myfun(a, b, c, x)&#8217;, &#8216;x&#8217;, &#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;);</p>
<p>Call FMINCON using this inline function as the first input and specify a, b, and c as additional parameters in FMINCON, after the options structure (the P1, P2, etc. inputs in the M-file help in versions prior to R14) just as described in this document from the Technical Support site:</p>
<p><a href="http://www.mathworks.com/support/solutions/data/1-19HM6.html?solution=1-19HM6" rel="nofollow">http://www.mathworks.com/support/solutions/data/1-19HM6.html?solution=1-19HM6</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Silas Leavesley</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-16285</link>
		<dc:creator>Silas Leavesley</dc:creator>
		<pubDate>Thu, 07 Jun 2007 16:17:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-16285</guid>
		<description>Thank-you for discussing some of these topics.

I have what may be a rather simple question, but does not seem to be discussed in the MATLAB documentation.  I am running an optimization, where my function to be optimized is saved as a separate m-file.  Something like:

[x,y] = fmincon(@myfun,x0,[],[],[],[],[],[],@funcon);

The problem is that I need to pass more than one variable to both myfun and to funcon, something like:

y = myfun(a,b,c,x)

As far as I can figure, when you call fmincon it only allows the variable x to be passed to @myfun.  Is there any other way to pass a,b, and c to the function as well?

Thanks,

Silas</description>
		<content:encoded><![CDATA[<p>Thank-you for discussing some of these topics.</p>
<p>I have what may be a rather simple question, but does not seem to be discussed in the MATLAB documentation.  I am running an optimization, where my function to be optimized is saved as a separate m-file.  Something like:</p>
<p>[x,y] = fmincon(@myfun,x0,[],[],[],[],[],[],@funcon);</p>
<p>The problem is that I need to pass more than one variable to both myfun and to funcon, something like:</p>
<p>y = myfun(a,b,c,x)</p>
<p>As far as I can figure, when you call fmincon it only allows the variable x to be passed to @myfun.  Is there any other way to pass a,b, and c to the function as well?</p>
<p>Thanks,</p>
<p>Silas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad Phelan</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-204</link>
		<dc:creator>Brad Phelan</dc:creator>
		<pubDate>Thu, 25 May 2006 07:33:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-204</guid>
		<description>fh = @(str, item)[str, ‘,’, item]

str and item ARE the arguments to the anonymous function. They are not defined in any workspace.

&gt;&gt; fh(&#039;XXX&#039;, &#039;YYY&#039;)
ans =
 &#039;XXX,YYY&#039;</description>
		<content:encoded><![CDATA[<p>fh = @(str, item)[str, ‘,’, item]</p>
<p>str and item ARE the arguments to the anonymous function. They are not defined in any workspace.</p>
<p>&gt;&gt; fh(&#8216;XXX&#8217;, &#8216;YYY&#8217;)<br />
ans =<br />
 &#8216;XXX,YYY&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-203</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Thu, 25 May 2006 00:26:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-203</guid>
		<description>Brad,

Thank you again.  It is a bit more clear.  From the documentation page for the function handle, I had read this section several times without understanding which set of arguments they were referring to:

The @ operator constructs a function handle for this function, and assigns the handle to the output variable sqr. As with any function handle, you execute the function associated with it by specifying the variable that contains the handle, followed by a comma-separated argument list in parentheses. The syntax is
fhandle(arg1, arg2, ..., argN)

I infer from your posting that &quot;arg1, arg2, ... argN&quot; are the input arguments for the function represented by  the function handle, &quot;fhandle.&quot;

But, when I look at my Example 1, I can&#039;t figure out what variable names to put in the argument list.  Looking at your original example, when fold is called, &quot;str&quot; and &quot;item&quot; aren&#039;t defined in the base workspace, they are only defined within the function workspace.  When you generated the anonymous function handle within the call for &quot;fold,&quot; you specified these internal variables for the function handle to operate on.  Therefore, you had to know all the internal details of the function &quot;fold&quot; before you could use the function handle.

A point of progress:  I think I now see how this works when using a function handle in a MatLab provided function, like &quot;fmindnd.&quot;  That function is limited to function handles representing functions with only one input argument, therefore my confusion regarding the input arguments doesn&#039;t apply. 

Your comments comparing this feature to C &amp; C++ reinforce my perception that MatLab developers are ace C &amp; C++ programmers.  The OOP features of these languages are billed at allowing: 1. Packaging data and operations together.  2. Dividing problems into manageable pieces.  3. Promoting reuse of code.  I have never worked on a project where 1 appeared to be an advantage and 2 &amp; 3 are easy to implement in a Fortran type language using functions and subroutines.  One of the great advantages of MatLab is that it was easy for me to engage with, from a Fortran perspective.  It appears that some of these other features that I&#039;m having trouble with are OOP type features and I&#039;m having trouble getting the concepts.

Thanks again.

Oliver A. Chapman, PE</description>
		<content:encoded><![CDATA[<p>Brad,</p>
<p>Thank you again.  It is a bit more clear.  From the documentation page for the function handle, I had read this section several times without understanding which set of arguments they were referring to:</p>
<p>The @ operator constructs a function handle for this function, and assigns the handle to the output variable sqr. As with any function handle, you execute the function associated with it by specifying the variable that contains the handle, followed by a comma-separated argument list in parentheses. The syntax is<br />
fhandle(arg1, arg2, &#8230;, argN)</p>
<p>I infer from your posting that &#8220;arg1, arg2, &#8230; argN&#8221; are the input arguments for the function represented by  the function handle, &#8220;fhandle.&#8221;</p>
<p>But, when I look at my Example 1, I can&#8217;t figure out what variable names to put in the argument list.  Looking at your original example, when fold is called, &#8220;str&#8221; and &#8220;item&#8221; aren&#8217;t defined in the base workspace, they are only defined within the function workspace.  When you generated the anonymous function handle within the call for &#8220;fold,&#8221; you specified these internal variables for the function handle to operate on.  Therefore, you had to know all the internal details of the function &#8220;fold&#8221; before you could use the function handle.</p>
<p>A point of progress:  I think I now see how this works when using a function handle in a MatLab provided function, like &#8220;fmindnd.&#8221;  That function is limited to function handles representing functions with only one input argument, therefore my confusion regarding the input arguments doesn&#8217;t apply. </p>
<p>Your comments comparing this feature to C &amp; C++ reinforce my perception that MatLab developers are ace C &amp; C++ programmers.  The OOP features of these languages are billed at allowing: 1. Packaging data and operations together.  2. Dividing problems into manageable pieces.  3. Promoting reuse of code.  I have never worked on a project where 1 appeared to be an advantage and 2 &amp; 3 are easy to implement in a Fortran type language using functions and subroutines.  One of the great advantages of MatLab is that it was easy for me to engage with, from a Fortran perspective.  It appears that some of these other features that I&#8217;m having trouble with are OOP type features and I&#8217;m having trouble getting the concepts.</p>
<p>Thanks again.</p>
<p>Oliver A. Chapman, PE</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: brad phelan</title>
		<link>http://blogs.mathworks.com/loren/2006/05/03/command-and-function-syntaxes-in-matlab/#comment-199</link>
		<dc:creator>brad phelan</dc:creator>
		<pubDate>Wed, 24 May 2006 07:48:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=35#comment-199</guid>
		<description>Oliver said:  
&gt; Unfortunately, this is where the MatLab documentation 
&gt; leave me hanging. I can’t discover the syntax for passing
&gt; multiple input arguments to the function represented by 
&gt; the function handle, “funhand.” Maybe you can help me 
&gt; with this.

The Matlab documentation is pretty clear on this. Once you have a function handle you can call it just like any other function.

% As a normal function
function a = add(x, y)
   a = x + y
end

&gt;&gt; fh = @add;
&gt;&gt; fh(1,2)
ans = 3

% or as an anonymous function
&gt;&gt; add = @(x,y) x + y;
&gt;&gt; add(1, 2)
ans = 3
 
Think about a function handle as a pointer to a function. I C/C++ you can do similar things with function pointers. Matlab function handles are easier to use and have more features than C function pointers.

Brad</description>
		<content:encoded><![CDATA[<p>Oliver said:<br />
&gt; Unfortunately, this is where the MatLab documentation<br />
&gt; leave me hanging. I can’t discover the syntax for passing<br />
&gt; multiple input arguments to the function represented by<br />
&gt; the function handle, “funhand.” Maybe you can help me<br />
&gt; with this.</p>
<p>The Matlab documentation is pretty clear on this. Once you have a function handle you can call it just like any other function.</p>
<p>% As a normal function<br />
function a = add(x, y)<br />
   a = x + y<br />
end</p>
<p>&gt;&gt; fh = @add;<br />
&gt;&gt; fh(1,2)<br />
ans = 3</p>
<p>% or as an anonymous function<br />
&gt;&gt; add = @(x,y) x + y;<br />
&gt;&gt; add(1, 2)<br />
ans = 3</p>
<p>Think about a function handle as a pointer to a function. I C/C++ you can do similar things with function pointers. Matlab function handles are easier to use and have more features than C function pointers.</p>
<p>Brad</p>
]]></content:encoded>
	</item>
</channel>
</rss>

