how to use a function that is not in the same folder as your current folder?
587 ビュー (過去 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?
0 件のコメント
採用された回答
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
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
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
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
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Search Path についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!