How to call a function of a matlab file in another matlab file ?

109 ビュー (過去 30 日間)
Shardul Pandey
Shardul Pandey 2015 年 6 月 18 日
コメント済み: Guillaume 2015 年 6 月 18 日
i have function P in "ideal.m" matlab file and i want to use P function in another "step.m" matlab file. how?

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 18 日
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.
For example,
function h = ideal(something, somethingelse, option)
if nargin == 3 and strcmp(option, 'GetP')
h = @P;
return
end
... regular code for ideal when not invoked with GetP option ...
end
function P(A, B)
...
function
Then step would call
P = ideal([], [], 'GetP');
and after that would be able to call
P(arguments...)
in any routine that had the P variable in scope.
  6 件のコメント
Walter Roberson
Walter Roberson 2015 年 6 月 18 日
"function P(A,B)" is the line in your ideal.m code that defines the P file you want to share with step.m. Use whatever arguments and declaration are appropriate.
Guillaume
Guillaume 2015 年 6 月 18 日
@Shardul, I hate to insist on this but as it looks like you're not very advanced in matlab (you do not know what nargin stands for), I would strongly encourage you to not go down the route of passing function handles to local functions.
It may solve your immediate problem but this is simply not the normal way of using function scope and unless you know what you're doing it's going to lead to problems in the future.
See Konstantinos' answer for a more detailed explanation than my answer. Use separate files and a 'private' folder if necessary.

サインインしてコメントする。

その他の回答 (3 件)

Konstantinos Sofos
Konstantinos Sofos 2015 年 6 月 18 日
編集済み: Konstantinos Sofos 2015 年 6 月 18 日
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
B.k Sumedha 2015 年 6 月 18 日
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 件のコメント
B.k Sumedha
B.k Sumedha 2015 年 6 月 18 日
Yes u can.
Walter Roberson
Walter Roberson 2015 年 6 月 18 日
"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
Guillaume 2015 年 6 月 18 日
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".

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by