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

446 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

Stephen23
Stephen23 le 11 Jan 2018
Modifié(e) : Stephen23 le 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 commentaires
Steven Lord
Steven Lord le 17 Juil 2022
You might be interested in Projects in MATLAB.
Adnan Saood
Adnan Saood le 18 Juil 2022
@Steven Lord Thanks, I will look into it

Connectez-vous pour commenter.

Plus de réponses (2)

vincent caillet
vincent caillet le 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 commentaires
Stephen23
Stephen23 le 19 Nov 2018
Modifié(e) : Stephen23 le 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 le 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.

Connectez-vous pour commenter.


Almog
Almog le 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.

Catégories

En savoir plus sur Search Path dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by