How to call a function of a matlab file in another matlab file ?
542 views (last 30 days)
Walter Roberson on 18 Jun 2015
In order to do that you have to code your function "ideal" of "ideal.m" to return a handle to the function P. Once you have the handle, you can invoke the function.
function h = ideal(something, somethingelse, option)
if nargin == 3 and strcmp(option, 'GetP')
h = @P;
... regular code for ideal when not invoked with GetP option ...
function P(A, B)
Then step would call
P = ideal(, , 'GetP');
and after that would be able to call
in any routine that had the P variable in scope.
More Answers (3)
Konstantinos Sofos on 18 Jun 2015
Edited: Konstantinos Sofos on 18 Jun 2015
The first function in an m-file (i.e. the main function), is invoked when that m-file is called. It is not required that the main function have the same name as the m-file, but for clarity it should. When the function and file name differ, the file name must be used to call the main function.
All subsequent functions in the m-file, called local functions (or "subfunctions" in the older terminology), * can only be called by the main function and other local functions in that m-file * . Functions in other m-files can not call them.
In addition, you can also declare functions within other functions. These are called nested functions , and these can only be called from within the function they are nested. They can also have access to variables in functions in which they are nested, which makes them quite useful albeit slightly tricky to work with.
More food for thought...
There are ways around the normal function scoping behaviour outlined above, such as passing function handles as output arguments as mentioned in Walters' answer. However, I wouldn't suggest making it a habit of resorting to such tricks, as there are likely much better options for organizing your files.
For example, let's say you have a main function A in an m-file A.m , along with local functions D , E , and F . Now let's say you have two other related functions B and C in m-files B.m and C.m , respectively, that you also want to be able to call D, E, and F. Here are some options you have:
• Put D , E , and F each in their own separate m-files, allowing any other function to call them. The downside is that the scope of these functions is large and isn't restricted to just A , B , and C , but the upside is that this is quite simple.
• Create a defineMyFunctions m-file (like in Walters' example) with D , E , and F as local functions and a main function that simply returns function handles to them. This allows you to keep D , E , and F in the same file, but it doesn't do anything regarding the scope of these functions since any function that can call defineMyFunctions can invoke them. You also then have to worry about passing the function handles around as arguments to make sure you have them where you need them.
• Copy D , E and F into B.m and C.m as local functions. This limits the scope of their usage to just A , B , and C , but makes updating and maintenance of your code a nightmare because you have three copies of the same code in different places.
• Use private functions! If you have A , B , and C in the same directory, you can create a subdirectory called private and place D , E , and F in there, each as a separate m-file. This limits their scope so they can only be called by functions in the directory immediately above (i.e. A , B , and C ) and keeps them together in the same place (but still different m-files)
All this goes somewhat outside the scope of your question, and is probably more detail than you need, but I thought it might be good to touch upon the more general concern of organizing all of your m-files.
B.k Sumedha on 18 Jun 2015
Guillaume on 18 Jun 2015
If P is not the main function in "ideal.m" (that is not the function declared at the top of the file that you would call with result = ideal()), then there is no easy way to call it. Such a function is either a local or nested function and the whole purpose of these is that they are only visible to the main function of the file.
The only way you would be able to call P from outside ideal is if ideal returns a function handle to P. Unless there is a very good reason to use a function handle (e.g. P is a callback function), then the proper way to make P accessible to more than one function is to have it in its own file. If you do not want P to be visible to functions other than ideal and step (and others in the same folder), then put it in a "private" folder below the one containing "ideal.m" and "step.m".