matlab creating paths to stop copying code - matlab

I have created a few general function in MATLAB that I intend to use for a few separate projects. However I do not want to copy the function into each separate project function.
I have created a folder called Misc_Function when I have placed these general functions. I know I can reference this functions explicitly by using the path and function name when trying to call the functions.
I believe you can add a path (in my case 'H:\MyTeam\Matlab\Misc_Function') when MATLAB loads up is that correct and if so how do you do this?
Assuming the above can be done I'm interested to know how MATLAB finds the correct function. In my understanding (guess work) MATLAB has a list of paths that it check trying to find a function with the name specified - is that correct? If so what happens when there are functions with the same name?

MATLAB indeed has its own search path which is a collection of folders that MATLAB will search when you reference a function or class (and a few other things). To see the search path, type path at the MATLAB prompt. From the documentation:
The order of folders on the search path is important. When files with the same name appear in multiple folders on the search path, MATLAB uses the one found in the folder nearest to the top of the search path.
If you have a set of utility functions that you want to make available to your projects, add the folder to the top of the search path with the addpath function, like so
addpath('H:\MyTeam\Matlab\Misc_Function');
You have to do this everytime you start MATLAB. Alternatively, and more conveniently, save the current search path with the savepath command or add the above commands to your startup.m file.

You can check the actual paths where Matlab searches for functions using
path
You will notice, that the most top path (on start up) is a path in your home folder. For Linux this is e.g. /home/$USER/Documents/MATLAB. For Windows it is somewhere in the the c:\Users\%USER%\Documents\Matlab (I think). Placing a file startup.m in this folder allows to add additional paths using
addpath('H:\MyTeam\Matlab\Misc_Function');
or
addpath(genpath('H:\MyTeam\Matlab\Misc_Function'));
on start up of Matlab. The latter (genpath) allows to also add all subdirectories. Simply write a file startup.m and add one of above lines there.

I believe 'addpath' will add the folder to MATLAB path only for the current MATLAB session. To save the updated path for other sessions, you need to execute 'savepath' command.
As mentioned in the previous comments, adding the folder in startup.m is a good idea since it will be added to the path on MATLAB startup.
To answer your question about how MATLAB finds the correct function, MATLAB maintains a list of directories in its path in a file called pathdef.m. Any changes to the path will be written to this file (when you execute 'savepath'). The path variable is initialized with the contents of this file.

Related

Using Function from User-Defined Script in MATLAB

I have placed a MATLAB script "x.m" in the path of my current working directory. So the script is in folder "~/a" and my working directory is "~/a/b"
But MATLAB seems to be not recognizing that the single function 'x' in the script exists? I have named the file exactly the same as function, which works properly when I place the script in my exact working directory rather than just on the path of my working directory. I believe my version is MATLAB2016a, if that makes any difference. Before updating to MATLAB2016a, I had made a similar function and was able to use it properly with just putting the script in the path of my working directory.
Suggestions/solutions?
You can't just have a function in the parent directory and expect MATLAB to find it. There is a thing called the MATLAB path, which is the collection of directories MATLAB will search to find functions. You can add directories to it using addpath.

Calling user defined function anywhere? (matlab)

Where can i save my document "function.m", so i can call this function from any code in any path of my pc?
I already made some custom functions, but i dont know where to save those codes to be able to call them from any part.
Save them in a logical place where you can find them back. Then, use addpath to add that folder to MATLAB's search path, i.e. when you use a function it will try that folder as well to find it.
You can do this either per script, or edit startup.m with this, so that it's automatically included when MATLAB starts.
Alternatively to Adriaan's best practice answer, the fastest option is to save your function.m in the %USERPROFILE%/Documents/MATLAB directory on your PC ($home/Documents/MATLAB on Linux and Mac). This directory is on your MATLAB Search Path by default. This might be an acceptable solution if you use the function commonly across different projects.
Finally, there is a "manual" solution: Execute pathtool in MATLAB, add the directory containing your function.m via the GUI and hit save.

setting a default matlab path at startup

My team is trying to standardise our Matlab paths so that everyone has the same.
I have a list of the default matlab path that we should all have.
So we would like to have a script that runs when matlab opens to make sure that our paths are set to the default matlab path. So if a path has been added to our default list it will be added in the correct place.
Is this possible in Matlab?
I read about startup but that seems to do with set your working directory which is different to what I am trying to do.
You can change which directory MATLAB starts in using the userpath function so that whenever you start up MATLAB, the path will automatically redirect here.
This may be useful if you have MATLAB running on a network per se, and multiple instances can start in the same network directory.
See more from MathWorks here: http://www.mathworks.com/help/matlab/matlab_env/matlab-startup-folder.html
However, if you want to standardize everything so that everyone has access to the same path, you can use startup to add directories / folders to MATLAB's path, but if you want to complete the package, use userpath to get MATLAB to start at a specified directory.
Your startup.m file may look something like this:
addpath('/folder/to/add/one');
addpath('/folder/to/add/two');
addpath('/folder/to/add/three');
addpath('/folder/to/add/four');
Then set your userpath with the function to complete everything:
userpath('/folder/to/start');
addpath('/folder/to/start');
Also make sure you add this new folder to your startup.m file too.
Include a path or addpath line in file startup.m. For example, to add folder aaa\bbb to the path the line would be
addpath('aaa\bbb')
Note that each user may have a different startup.m file. You may need to create it, if it doesn't already exist.

relative path for a MATLAB program called via the search path

I wrote a MATLAB program with a GUI (to enter the measurement settings) and a measurement function which gets called when pressing "START" in the GUI.
In both I use separate files for sub functions to keep it easier to read and maintain.
The file structure looks something like this
C:/../folder/+measure/measure.m
C:/../folder/+measure/getData.m
C:/../folder/+measure/plot.m
C:/../folder/+measure/evalutate.m
C:/../folder/+measureGUI/getGuiData.m
C:/../folder/+measureGUI/calcLimits.m
C:/../folder/+measureGUI/saveGuiState.m
C:/../folder/+measureGUI/loadGuiState.m
C:/../folder/+measureGUI/background.png
C:/../folder/+measureGUI/guiState.mat
C:/../folder/measureGUI.fig
C:/../folder/measureGUI.m
This works, if I'm executing the measureGUI.m in "folder".
The current settings in the GUI are saved in the guiState.mat file when closing the GUI in saveGuiState.m
filename = '+autoProberGUI/guiState.mat';
save(filename, 'guiState');
And loaded (by loadGuiState.m) the next time the GUI gets opened.
Now I have to put the finished program on a network drive and add the folder to my matlab search path to call measureGUI.m.
The program works but it can't save or load the guiState.mat due to the relative path (I guess the path is relative to the folder I'm currently in, and not the folder the calling function is in).
I think I could include the subfolder to the search path or use an absolute path in filename. But both solutions seem to me to be not the proper way.
Is there a way to have relative paths to the file from where the function is located on the drive? Meaning relative to
I:/..NetworkDrive../folder/+measureGUI/saveGuiState.m
instead of relative from where I call measureGUI.m
(Sorry for the poor English, I hope it is not too confusing)
You can use pwd to get the full path to you current working directory.
Then you can concatenate with [pwd '/folder/+measureGUI/saveGuiState.m'].
To locate the function you can use which.

automatically add path in a MATLAB script

I have several MATLAB scripts to share with my colleagues. I have put these scripts under a specified directory, e.g., /home/sharefiles
Under the MATLAB command prompt, the users can use these scripts by typing
addpath /home/sharefiles
Is there a way to automatically add this path in my matlab script, and save users the efforts of invoking addpath /home/sharefiles each time.
Sure, just add the addpath to your script.
addpath('/home/sharefiles')
If you want to recursively add subdirectories, use the genpath function:
addpath(genpath('/home/sharefiles')
Adding files to the path or one of the slower operations in Matlab, so you probably don't want to put the addpath call in the inner loop of an operation. You can also test to see if you need to add the path first.
if ~exist('some_file_from_your_tools.m','file')
addpath('/home/sharefiles')
end
Or, more directly
if isempty(strfind(path,'/home/sharefiles;'))
addpath('/home/sharefiles')
end
You could add the code posted by Pursuit to your startup.m file so that MATLAB adds it to the path automaticlly upon startup. Or, take a look at the savepath function. Lastly,
So when you Use the GUI to set path, the paths get added in the default start directory of Matlab in the pathdef.m file present there. Hence if you are running your code from any other directory either you would have to copy over this file or create a script in the startup folder. Hope this helps!!