I love dropbox. I love being able to have all of my files on every computer I use, both personal and work machines. And overtime I have found some useful fixes and hacks to allow an even more seamless transition from one computer to another. This normally involves linking configuration files, however for some programs a bit more is needed. One example is MATLAB.
I find MATLAB a really useful environment for data analysis, calculations, and performing a number of repetitive tasks. As part of this I have written a number of my own functions which I use regularly. Secondly, I often have programs which load in data from other files stored in my dropbox, so the full path is different on different machines. Using relative paths is only a half fix as I don’t always run programs in their folders. Also my home computers are now on linux (Ubuntu and Lubuntu) while my work machine is on Windows. So here is how I have configured my MATLAB.
Setting the same starting directory.
I have a folder called MATLAB_home in my dropbox. So if I am ever lazy and write a quick program and save it in the local directory it will always be in dropbox and in the local directory when I open MATLAB elsewhere.
To set the starting directory in Windows you simply right click on the MATLAB icon, select properties, and then enter the full path to your MATLAB_home folder in dropbox. Then click OK.
In Ubuntu/Lubuntu I use MainMenu (a useful program for controlling menu items) to change or (or create if it was not created during install) the MATLAB launcher. In the section labelled command I have changed the command to (%s are part of the path, don’t add %s):
sh -c "cd %PathToMatlabHomeFolder% &&%PathToMatlabExecutable% -c %PathToMatlabLicenceFile% -desktop"
This needs to be done on each machine.
Set up a functions directory
Now, over time you have probably written, been sent, and downloaded a number of MATLAB functions which you use regularly. I know many people who simply copy these to any new working directory leaving them with multiple copies of the same files in many locations. This is not only time consuming, it is also a silly idea, if you ever update anything in these functions then everything becomes out of sync.
As many of you know MATLAB has a path, similar to the path in unix or windows. For those who don’t know what this means. Essentially any time you call a function MATLAB checks through every folder in the path to see if that folder has the function you called.
So now make another directory in dropbox (or you can just use your MATLAB_home directory if you prefer). I called mine MATLAB_ROOT. Now we need to run MATLAB as an administrator/superuser if we are to permanently change the path (In windows XP or before you don’t need to run as an administrator).
Once MATLAB opens. Select “Set Path…” from the File menu. In select “Add Folder” and select your new functions folder. Click “OK”. Then in the Set Path window click “Save”, then “Close”.
Again you need to set the path on every computer you use. Now you can put your own functions in this folder and use them from any folder on any of your computers.
Dropbox friendly file paths in MATLAB
So, now imagine you are writing a program which opens files from a number of different folders in your dropbox. I do this by making a new function which returns my dropbox path for the computer I am on. I identify my computers using their names, MATLAB can get the name of a computer by using the following command
which will save the computer’s name to “SYSname” and a 0 to “blank”, (in newer versions of MATLAB blank can be replaced by a ~ character as it is not needed). The SYSname variable returned always has an extra character on the end which displays as a space but screws up string matches, so we ignore it.
[blank, SYSname] = system('hostname');
Now make a new file an paste in the following code:
function path = DropboxPath
[blank, SYSname] = system('hostname');
path = '/home/gobaskof/Dropbox';
path = 'G:\Documents\Dropbox';
error('Computer not recognised')
This is an example with two of my computers (Oliphant is a very large laptop, Prometheus is my powerful work PC, but the university IT people regularly metaphorically pick out its liver.). Replace the name with the names of your computers, and the paths with the paths to your dropbox folder on that computer. Repeat the elseif block for any extra computers.
Now save this file as DropboxPath.m in your newly created functions folder.
So now if you want to open the file ‘file.dat’ which is in the folder ‘Data’ of your dropbox you can simply run:
This will new work on any of your (Windows) computers. Replace / with \ and it will work on any of your UNIX computers. (Below I will discuss how to make thing run if you use both Windows and UNIX).
FID = fopen([DropboxPath,'\Data\file.dat'],'r');
At this point it is worth mentioning, if you ever want to output things in a your computer’s temporary folder you come to a similar problem. MATLAB has a function for this called ‘tempdir’ which returns the current computer’s temporary folder.
Paths which work in UNIX and Windows
Paths in unix use / to separate folders, Windows uses \, so this can be a problem. It is possible to get around this by using the Matlab command ‘fullfile’, which accepts string inputs for every folder and the file and returns a string with them all linked together with the correct file separator. i.e.:
For long file paths this is a ball-ache. Also the MATLAB function ‘filesep’ comes in handy, it simply returns / in UNIX and \ in Windows, but again always using this in every file path is a annoying. So instead I have made another simple function (again saved in the new function directory in dropbox), this one corrects all file paths to the current operating system:
function path = CorrectFilePath(path)
pos = strfind(path,'\');
path(pos) = '/';
pos = strfind(path,'/');
path(pos) = '\';
Once this is saved in your functions folder you can use the following syntax to open a file and it will work on both UNIX and Windows computers:
which is much more convenient for files buried many folders deep in your dropbox as you can just copy the path on whichever computer you are using at the time.
FID = fopen([DropboxPath,CorrectFilePath('\Data\file.dat')]),'r');
That’s all folks
And that’s it. Using the same MATLAB code on multiple computers should now be pain-free.