How to call a function of a matlab file in another matlab file ?
Show older comments
i have function P in "ideal.m" matlab file and i want to use P function in another "step.m" matlab file. how?
Accepted Answer
More Answers (3)
Konstantinos Sofos
on 18 Jun 2015
Edited: Konstantinos Sofos
on 18 Jun 2015
5 votes
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.
Regards,
B.k Sumedha
on 18 Jun 2015
1 vote
U can take a look at this one.Is it the same u want to know? https://in.mathworks.com/matlabcentral/answers/222005-2-m-file-interaction
7 Comments
Shardul Pandey
on 18 Jun 2015
Guillaume
on 18 Jun 2015
Please! do not use text speak on the forum.
B.k Sumedha
on 18 Jun 2015
Edited: B.k Sumedha
on 18 Jun 2015
Say that u have a function something like
function[a,b] = idel(x)
In ur another m file u can use this function as
.....
....
[a,b] = idel(coord_1);
....
..
Walter Roberson
on 18 Jun 2015
You should strongly avoid calling your own routine "sum", unless you are defining a "sum" method in an object class.
Shardul Pandey
on 18 Jun 2015
B.k Sumedha
on 18 Jun 2015
Yes u can.
Walter Roberson
on 18 Jun 2015
"run" is the name of a MATLAB library routine to execute script files. Re-using it is less bad than re-using "sum", but it can still be confusing.
Guillaume
on 18 Jun 2015
0 votes
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".
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!