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

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?

 Accepted Answer

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 Comments

I know I can use
addpath folder_where_function_is_located
to add the folder to the searchpath, which allows me to use the function. But the problem is that MATLAB and I don't know where this function is located. If I use
which isittrue
MATLAB says: 'isittrue' not found. That is because the folder where this function is located is not added to the searchpath.
I somehow have to search to every .m file in my entire pc to find the isittrue function. But I don't know the command in MATLAB to do this.
"That is a really bad reason to run code in a particular folder."
Let's say I store measured data on my pc. Using
[filename,pathname] = uigetfile('*.mat','Select the MATLAB code files', 'MultiSelect', 'on');
cd(pathname);
I select the data and MATLAB set the folder where my data is at the current folder. Now I am able to use the data in my script. What I could do, or maybe should do? is using
addpath (pathname)
Is that what you mean?
After that, I load the data and do stuff. Then I want to use my function on the data, but this function is not in the same map as my current folder, thus MATLAB cannot use this function. Here comes in that MATLAB has to locate the folder where the function is in and add that to the path.
Hopefully you understand what I want to accomplish.
"Let's say I store measured data on my pc"
Good idea.
" cd(pathname);"
Do NOT use cd to change the directory: this is slow, and it makes debugging more difficult. I recommend that you avoid cd except perhaps in the command window. You should use the full filename (i.e. relative or absolute name) to load the data, wherever it may be on your computer, without changing the current directory. This is explained in the first link that I gave you. Did you read it?
" addpath (pathname) Is that what you mean?"
No. I would not recommend changing the MATLAB path in order so that it includes all of your data directories. The MATLAB path should be tightly controlled to include the MATLAB installation and a few of your directories where your code is saved. Where your code is saved should not be polluted with hundreds of data files (some might disagree on this... they are welcome to comment below).
Use the fullname to load that data. Do not change the MATLAB path for processing data files.
"This function is not in the same map as my current folder, thus MATLAB cannot use this function. Here comes in that MATLAB has to locate the folder where the function is in and add that to the path."
Nope. You should manually add that directory to the MATLAB path, before running the function by calling it normally.
This requires knowing where the code directory is. It is not possible for MATLAB to magically know everything that is saved on your computer and/or all of the attached drives/servers/clouds/backups/..., or to search all of those each time you want to run something. Such an idea is intractable, because simply searching those drives for some script that might exist somewhere would take hours/days/weeks/months/years/centuries/eons/...
Matlab like many programs expect its code to be in specific folders. Matlab is actually quite flexible in that there can be many code folders and you can easily add new folders with addpath. Code however has to be in one the folder on the path.
So the problem is that your expectation is that the code can be put in any folder. I'd recommend changing that expectation. Your code should reside in a fixed (I'd recommend a subfolder of the matlab directory in My Documents on windows) and whomever you give the file to should put that file in a folder on the path or ensure that the folder where they put that file is added to the path. That's the way matlab work. Trying to change the way it works to conform to your expectation is asking for trouble.
And Stephen is absolutely correct, the code and the data should not be in the same folder. They should be completely separated. uigetfile gives you back the full path of the selected files. You then use that full path to access that data. That means you don't cd in the data directory, and you don't add that data directory to the path. e.g.:
[filename, pathname] = uigetfile('*.mat','Select the MATLAB code files', 'MultiSelect', 'on');
for fileidx = 1:numel(filename)
filecontent = load(fullfile(pathname, filename{fileidx})); %load using full path
%do something with filecontent
end
It's clear to me now that I have to put the .m files in the MATLAB folder. If I do, MATLAB can indeed see this function. Thanks for that.
"This is explained in the first link that I gave you. Did you read it?"
Yes, I read it. There was no explanation about searching in your pc, instead of only the MATLAB folder. That's why I asked it.
So, as long as the files/ data/ functions are in the MATLAB folder, MATLAB is able to find them and do calculations on them, even if the current folder is not the same as these files/ data or functions are in. That makes sense.
"the code and the data should not be in the same folder."
I don't have the data and program in the same folder. That's why I changed the current folder to the data. But with fullfile, it can read in anyways. THanks
I find using the path is not a good practice, especially in large projects with 20+ functions; it is not comfortable to add everything everywhere and keep track of it.
Maybe some sort of project config files like Qt .pro file would make it better.
Is there to your knowledge something similar in Matlab?

Sign in to comment.

More Answers (2)

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 Comments

"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?
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.

Sign in to comment.

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.

Categories

Community Treasure Hunt

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

Start Hunting!