How to call function in different file path

조회 수: 225 (최근 30일)
Khang Nguyen
Khang Nguyen 2023년 5월 10일
댓글: 佳禹 2024년 9월 23일
hi,
I am currently working on a big project, to organize it I want to create many folder and put the functions in it. for example:
if I want to write code on work.m that is in the work folder. How can I call fun1.m and fun2.m that is in different folder? I could not find anything useful online so I hope someone can help.
Thank you.

답변 (1개)

Shuba Nandini
Shuba Nandini 2023년 5월 29일
Hello,
It is my understanding that you want to call ‘fun1.m[SK1] and ‘fun2.m’ that is in different folder by the ‘work.m’ file which is defined in work folder.
You can call the function directly from the ‘work.m’ file by adding that file to the search path. If you want to add multiple folders to the search path, you can use the “addpath” command.
Please refer to the below link to know more about “addpath” command:
Hope this helps!
  댓글 수: 1
佳禹
佳禹 2024년 9월 23일
Thanks for your information.
I have taken an offline training provided by MATWORK arround 25/08/2024, in China
At that moment, your fellows gave me a folder where the example for the training is located in.
In that folder, callable functions are located in different folders. It seems that there is a file defined the locations of these function, so that after I executed such this file, I can call the function in different folders.
I forgot to ask them how to achieve it, because at that time I was in hurry to catch my train.
This kind of function is useful for a big project where there are so manny '.m' functions.
As you said, one way is added all the '.m' functions to the search path. However, next time, when you build new ',m' files, you should avoid using the same name. It is not convenient.
Is there existing some kind of method can tempororaily add some files into search file. After the Matlab is closed, every setting will get recovered.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by