<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/254049</link>
    <title>MATLAB Central Newsreader - Calling function in one m-file from another m-file</title>
    <description>Feed for thread: Calling function in one m-file from another m-file</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, 18 Jun 2009 13:14:02 -0400</pubDate>
      <title>Calling function in one m-file from another m-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/254049#658442</link>
      <author>beda meda</author>
      <description>Hi,&lt;br&gt;
I have two m-files. One is test.m and it contains this function:&lt;br&gt;
function x = test&lt;br&gt;
&lt;br&gt;
Another is target.m and it contains two functions:&lt;br&gt;
function result = target(a)&lt;br&gt;
function answer = start(b)&lt;br&gt;
&lt;br&gt;
Both m-files are in one directory. Now I know that when I write in test.m&lt;br&gt;
function x = test&lt;br&gt;
given_result = target(param);&lt;br&gt;
&lt;br&gt;
it will launch whatever is in fuction target(a) which is in target.m. But how can I call from m-file test.m function start(b) in target.m? Something like this&lt;br&gt;
&lt;br&gt;
function x = test&lt;br&gt;
given_answer= start(param);&lt;br&gt;
&lt;br&gt;
but this will obviously not work.&lt;br&gt;
&lt;br&gt;
Thanks.</description>
    </item>
    <item>
      <pubDate>Thu, 18 Jun 2009 13:31:12 -0400</pubDate>
      <title>Re: Calling function in one m-file from another m-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/254049#658447</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;beda meda&quot; &amp;lt;b.meda@centrum.cz&amp;gt; wrote in message &lt;br&gt;
news:h1deiq$rsn$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; I have two m-files. One is test.m and it contains this function:&lt;br&gt;
&amp;gt; function x = test&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Another is target.m and it contains two functions:&lt;br&gt;
&amp;gt; function result = target(a)&lt;br&gt;
&amp;gt; function answer = start(b)&lt;br&gt;
&lt;br&gt;
Since the name of the file is target.m I assume the target function is the &lt;br&gt;
first one in the file and is therefore the primary function in that file, &lt;br&gt;
with start being a subfunction.&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-39629.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-39629.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70666.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70666.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&amp;gt; Both m-files are in one directory. Now I know that when I write in test.m&lt;br&gt;
&amp;gt; function x = test&lt;br&gt;
&amp;gt; given_result = target(param);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; it will launch whatever is in fuction target(a) which is in target.m. But &lt;br&gt;
&amp;gt; how can I call from m-file test.m function start(b) in target.m? Something &lt;br&gt;
&amp;gt; like this&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function x = test&lt;br&gt;
&amp;gt; given_answer= start(param);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; but this will obviously not work.&lt;br&gt;
&lt;br&gt;
As stated in the second of the pages I linked above:&lt;br&gt;
&lt;br&gt;
&quot;M-files can contain code for more than one function. Additional functions &lt;br&gt;
within the file are called subfunctions, and these are only visible to the &lt;br&gt;
primary function or to other subfunctions in the same file.&quot;&lt;br&gt;
&lt;br&gt;
If you want to call the subfunction from outside its file, there are three &lt;br&gt;
main options.&lt;br&gt;
&lt;br&gt;
1) Move the subfunction to its own file as the primary function in that &lt;br&gt;
file -- then it will be visible to other functions.&lt;br&gt;
2) Call the primary function in the file and have it return a function &lt;br&gt;
handle to the subfunction, as described in the third paragraph of the &lt;br&gt;
reference page for function handles.  Then call the subfunction using that &lt;br&gt;
function handle.&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/function_handle.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/function_handle.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
function fh = target&lt;br&gt;
fh = @start;&lt;br&gt;
&lt;br&gt;
3) Call the primary function in the file and have it call the subfunction &lt;br&gt;
directly.  This is often called a &quot;switchyard&quot;, as it's often implemented &lt;br&gt;
using SWITCH:&lt;br&gt;
&lt;br&gt;
function output = target(functionToCall, varargin)&lt;br&gt;
switch functionToCall&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'start'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output = start(varargin{:});&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;otherwise&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Do other stuff&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Alternately, instead of SWITCH, you can use FEVAL:&lt;br&gt;
&lt;br&gt;
function output = target(functionToCall, varargin)&lt;br&gt;
output = feval(functionToCall, varargin{:});&lt;br&gt;
&lt;br&gt;
Personally I prefer option 1 in most cases, although I have used the other &lt;br&gt;
two on occasion.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Thu, 18 Jun 2009 13:57:01 -0400</pubDate>
      <title>Re: Calling function in one m-file from another m-file</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/254049#658464</link>
      <author>beda meda</author>
      <description>Many thanks Steve! This is exactly what I need.</description>
    </item>
  </channel>
</rss>

