3

I have written a Matlab function that I wish to use multiple times. To do so, at the moment I am copying the function into the appropriate directory prior to use.

The problem with this method is that its too easy to get lost in different versions of the function. Basically, I'm in a situation where I can't remember which version contains which modifications.

So my question is, how can I have a single version of the function that I can use from any directory?

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • [`Access m-files in a subfolder without permanently adding it to the path`](http://stackoverflow.com/questions/22013875/access-m-files-in-a-subfolder-without-permanently-adding-it-to-the-path) might be worth a read. – Divakar Jan 13 '16 at 17:11

3 Answers3

5

An easy way is to add the folder which contains this function to the search path, using addpath:

addpath('../a/relative/path')

or

addpath('/An/absolute/path/on/linux')
addpath('C:/an/absolute/path/on/windows')

As suggested by @brodoll in a comment, the genpath function, which creates a list of all subfolders, is useful, so not only the specified folder, but also all subfolders are added to the path:

addpath(genpath('path/to/the/folder'))

You can call this at the start of each script which uses this function.

Note: This adds this folder to the search path for the current MATLAB session. So if you run a script twice, you only need to add the path the first time. However after restarting MATLAB, you'll need to add the path again.

hbaderts
  • 14,136
  • 4
  • 41
  • 48
  • 1
    Easiest + safest, definitely do not want to move your function all over the place and duplicating them. – GameOfThrows Jan 13 '16 at 15:37
  • So to clarify, the `addpath` function is one time use, and when you restart Matlab you must add the path again to use it? Or does `addpath` permanently add the path? thanks – CiaranWelsh Jan 13 '16 at 15:42
  • Yes, exactly. I'll add that to the answer. – hbaderts Jan 13 '16 at 15:43
  • 3
    I'd just like to add a combo that I like:`addpath(genpath('/my/path/'))` which adds all folders under my/path to Matlab's path. It is handy depending on how you organize your functions – brodoll Jan 13 '16 at 15:43
  • 1
    @brodoll very nice, I didn't know about that. I added this to the answer. – hbaderts Jan 13 '16 at 15:47
1

Another quick method (if you don't want to mess with your matlab path, and also assuming you've never messed with it before) is to simply save that particular .m file to the "MATLAB" folder (which is the very first entry in a default matlab path, and the folder Matlab starts in when you open the program, again this is with default settings)

in windows its in C:\users\your_user_name\Documents\MATLAB

Mac OS X is /Users/your_user_name.Documents/MATLAB I've never used matlab on linux but I'm pretty positive its the same path as OSX uses

andrew
  • 2,451
  • 1
  • 15
  • 22
0

A quick way to do this would be to first create a directory on your system say mFunctions, where you can include the m-file for this function you wrote (and possibly others in the near future), and then add the directory mFunctions to the Matlab PATH.

On starting Matlab each time, locations in PATH are accessed, and m-files, simulink models etc. are made available just as the way you desire.

Check this link for the Matlab documentation on how to do this.

Anup Puri
  • 65
  • 6