필터 지우기
필터 지우기

how to use a function that is not in the same folder as your current folder?

조회 수: 709 (최근 30일)
The situation is: I have made a function 'isittrue.m'. I save this function somewhere, unknown, on my pc (or I give this .m file to a friend). In my script, I want to use this function, so I want to check in my script where this function is saved on my pc (or on my friend's pc) and then make this function usable (independent on the location of this function). The current folder has to remain the same, because I use data from this folder.
How do I do this?

채택된 답변

Stephen23
Stephen23 2018년 1월 11일
편집: Stephen23 2018년 1월 11일
That is exactly what the MATLAB path is for: change the MATLAB path to include the folder where that file is saved.
"The current folder has to remain the same, because I use data from this folder."
That is a really bad reason to run code in a particular folder. Once you start using relative and absolute paths then you have no restriction on where the data needs to be. All MATLAB functions that accept filenames also accept absolute filenames, so there is no excuse not to use them.
  댓글 수: 7

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

추가 답변 (2개)

vincent caillet
vincent caillet 2018년 11월 18일
You should try to use the function fileparts. It acts like "cd ../", by going into the previous folder and dynamically adds folders to the path without changing the current folder.
Current_Folder = pwd;
disp('Current_Folder')
>> Current_Folder = 'C:\SVN\Folder\MyFolder'
TopFolder = fileparts(pwd);
disp(TopFolder)
>> Current_Folder = 'C:\SVN\Folder\'
Top_TopFolder = fileparts(fileparts(pwd));
disp(Top_TopFolder)
>> Current_Folder = 'C:\SVN\'
Let's say your code is located in
>> Current_Folder = 'C:\SVN\Folder\YourCode.m'
The good news is that you can now do the following:
addpath(genpath([fileparts(fileparts(pwd)), filesep, 'YourCode.m' ]));
  댓글 수: 2
Stephen23
Stephen23 2018년 11월 19일
편집: Stephen23 2018년 11월 19일
"It acts like "cd ../", by going into the previous folder and dynamically adds folders to the path without changing the current folder"
fileparts does not add anything to the MATLAB Search Path, nor does it change directory.
"The good news is that you can now do the following:"
addpath(genpath([fileparts(fileparts(pwd)), filesep, 'YourCode.m' ]));
Why do you add 'YourCode.m', when addapath only accepts folder names?
Guillaume
Guillaume 2018년 11월 19일
and filepart also does not change the current directory. It does not acts like cd at all.
I do not understand the point of genpath in the provided code either. Either the path created is valid, in which case genpath will have no effect, or the path is not valid, in which case a different path than what was expected would be added to the path.

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


Almog
Almog 2019년 11월 24일
I know it's a bit old, and one answer has already been accepted.
However, regarding the specific request:
so I want to check in my script where this function is saved on my pc,
You can use the function which:
which FUNCTION_TO_QUERY
Where FUNCTION_TO_QUERY is the fucntion you want to check.

카테고리

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