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.

Answers


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!!


Need Your Help

Mount shared folder (vbox) as another user

ubuntu virtualbox ubuntu-10.04 mount

I'm sorry to ask this in here, but I tried serverfault and didn't get any answer for 4 days.

How to get only images using scandir in PHP?

php directory-structure file-extension scandir image-file

Is there any way to get only images with extensions jpeg, png, gif etc while using