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

601 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Stephen23
Stephen23 am 11 Jan. 2018
Bearbeitet: Stephen23 am 11 Jan. 2018
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

vincent caillet
vincent caillet am 18 Nov. 2018
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 Kommentare
Stephen23
Stephen23 am 19 Nov. 2018
Bearbeitet: Stephen23 am 19 Nov. 2018
"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 am 19 Nov. 2018
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.

Melden Sie sich an, um zu kommentieren.


Almog
Almog am 24 Nov. 2019
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.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by