How to call a function of a matlab file in another matlab file ?
조회 수: 103 (최근 30일)
이전 댓글 표시
i have function P in "ideal.m" matlab file and i want to use P function in another "step.m" matlab file. how?
댓글 수: 0
채택된 답변
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
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
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
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,
댓글 수: 0
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
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
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".
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!