Effectively using MATLAB with Dropbox

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
[blank, SYSname] = system('hostname');
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.

Now make a new file an paste in the following code:

function path = DropboxPath

[blank, SYSname] = system('hostname');

if strcmp(SYSname(1:end-1),'Oliphant')
    path = '/home/gobaskof/Dropbox';
elseif strcmp(SYSname(1:end-1),'Prometheus')
    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:
FID = fopen([DropboxPath,'\Data\file.dat'],'r');
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).

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)

if isunix
    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:
FID = fopen([DropboxPath,CorrectFilePath('\Data\file.dat')]),'r');
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.

That’s all folks

And that’s it. Using the same MATLAB code on multiple computers should now be pain-free.

11 thoughts on “Effectively using MATLAB with Dropbox

  1. Hi Julian,

    You seem to know quite a bit about dropbox and matlab, so I wonder if you can answer my question? In return, I’ll offer any assistance I can give in discrete mathematics. (Probably not so useful to you. :)) I’m a researcher in that at any rate.

    On my Mac, matlabR2009a runs a program off of a dropbox folder and saves its data to a file in the same folder. Naturally, that data file shows up in all the dropbox folders.

    A windows 7 PCs running matlabR2013a share that folder. When the PC does the mirror image, everything goes fine but the save results in an error.

    save(‘filename’,’variablename’) line in .m file on Windows PC results in

    Error using save
    Unable to write file
    No such file or directory.

    As said, the exact same setup but run on my Mac works perfectly.

    If the exact thing I need to do is in your post, could you point me to it? The level of explanation is super high for me I’m afraid.


    I guess it’s a stupid question, but there must be no way to use today’s technology to get STEM-level imagery of molecular structure in their *insides*? Maybe there’s something interesting going on in there that’s different than on the surfaces, which is all we can image right now I guess. Maybe there’s stuff going on inside that’s not quite expected, something about fault lines through regular crystalline structure but in 3D. If fault “planes” can only meet up in certain ways, perhaps this says something about a materials strength. I’m babbling of course.

  2. Oooh, this might not have been a good question for you after all. I just tried to have matlabR2013a on the window 7 PC run on a copy of the same file, extracted from dropbox and put into the outside environment and it still gets an error.

    The thing is it used to run on the Window 7 PC. Perhaps the matlab version updated while I didn’t notice?

    save(‘file’,’var’) is pretty basic and everything that important in matab, must be handled by all future versions, so I still don’t get it.

    • Hi Clifford,

      I am slightly confused by exactly what your problem is. So you are in on windows 7, Matlab’s working directory is somewhere in your drobpox, and you are having problem running the save command? And command is in a program?

      I would suggest a couple of little things. First check your working directory is where you think it is. Sometimes matlab may open in a working directory in an area that doesn’t have write permissions. If you then run the program in cell mode you might get the error. Second, check if you can run the save command in another folder (I have heard stories of certain commands not working in some installs of Matlab 2013). Finally, when you run
      does the filename end in .mat? It should automatically append the ‘.mat’, but maybe there is a bug in a new version. Seems unlikely.

      If this doesn’t solve it, I’d be interested to know. Perhaps with a few more details about when save does and doesn’t work. And where your working directory is.

      On the other question about internal molecular structure. For this people use diffraction techniques. This can also be combined with NMR to give a nice 3D interpretation of structure of large molecules.

      I’ll be in contact next time I have a discrete mathematics problem!

  3. Hi Julian,

    I think I have a problem with my collaborator’s computer which is a Windows 7 PC running Matlab2013a. Dropbox might be fine. Here’s what going on now. I have a directory Code on the desktop and it is outside of any Matlab directory I think. It’s not in a dropbox directory.

    I open a matlab workspace, move to the directory Code, and run a script from the matlab window command line. The script boils down to:

    calculate variable var
    save(‘savefile’, ‘var’)

    I get the following error:

    Error using save
    Unable to write file
    No such file or directory.

    I was able to do exactly this maybe a couple of months ago and everything went fine . (This is what every clueless person says, but I swear it’s true.) The script ran, the variable got calculated, the savefile got created, no problems.

    Perhaps something concerning Matlab was updated in the meantime. (It’s not my computer and the university might be managing the Matlab installation.)

    I anticipate that the next thing to do would be to try to do the same thing in the Matlab path. Since I have not paid any attention to paths and am just running matlab on scripts all over the place it might take me a while to even figure out where the Matlab directory is.

    Getting back to your post on putting the matlab path in dropbox. I’m sure I don’t want to unite my collaborator’s and my matlab paths. It worked before, but perhaps not being on the path became an issue between then and now? I could fix that by augmenting the path, right? The path is really a list of directories, rather than a directory?

    I have a Code directory in my dropbox directory and I shared it with my collaborator’s dropbox which appears in his machines. I want to edit the scripts in my Code directory and have the changes appear everywhere. Then I would get into each of my collaborator’s machines, run matlab on one of the scripts various scripts in the dropbox copies of the Code directory. These are supposed to save their results to the same Code directory. Those save files would then appear everywhere.

    The scripts are long-running. This way I can just wait for the results to appear without logging into all of my collaborator’s machines all the time. The only tedious part is getting the runs going on my collaborator’s machines. I don’t know what to do about that.

    As I said, though, I’m stuck on getting to save period on my collaborator’s machines.

    Fixing the save issue on my computers is way out the scope of what you were writing about. If you have an immediate idea though, please let me know. Beyond that, please don’t feel obligated to try to see it through.

    • So to try to answer your question on what exactly the path is. It really is just a list of directories. So when you run a function like ‘save’ or ‘plot’, matlab will search through (in the order they are listed) each of these directories for a file names ‘save.m’ or ‘plot.m’. So it is originally set up to include the directories of the core matlab functions and all of your toolboxes. What I do with dropbox is make another directory in dropbox and add it to the path, then when I write my own functions matlab can find them no mater which directory I am working in.

      The path shouldn’t affect issues such as saving. Unless you have written a function of your own called save.m and it is somewhere in your path or the folder Code. This would then be run instead of matlab’s own ‘save’ function. You can check this isn’t happening by running:
      the returned string should be something in your program files directory.

      I think the first place to start is just open Matlab fresh, navigate to the Code directory. And run in the command window:
      does this work?
      If not, move directory to somewhere else perhaps just your documents folder and try again (in case there is somehow a permissions problem for an unknown reason).

      This should give you a starting point to track down the problem. Tell me how you get on. Good luck

      • Hi Julian,

        I’ve fixed it. It was pretty stupid. The data gathering runs have a ton of parameters. Since we want to look at them later we put them in the save file name and these names were getting too long to be handled, hence the errors. I suppose we should save the parameters to the file and search for the data from a run with a particular set of parameters by searching for the run some other way like grep or something to look at the contents of the file. Since I don’t know well how to do that in UNIX and have not even a clue to do that in Windows, I might keep the file name approach.

        It will work for now, but another approach that could be very useful would be to save in a format that would allow concatenation of these save data files. Then a search within a single file would be what was needed. I am going massively parallel on a cluster with this soon, so I probably do need to make this.

        In my case I don’t think it would make sense to have a path including all my matlab in dropbox. I have many versions of my mini-library, all worthy of maintaining, it’s not a strict progression. The programs are called the same from directory to directory since it seemed more natural to indicate their version, by directory. The ones in a fixed version interact with each other so keeping it all straight via filenames is probably not as good. I am assuming once these directories are all on the same path, it would just use the first file if they are repetitively named.

        For now I am doing testing, and we’ll see if the dropbox solution is good enough for that for now.

        Thanks! Sorry my thing wound up being perpendicular and practically trivial.

        • This might be a silly question, but can’t you just save all of your parameter variables in the same save command? i.e.
          If you want later to open the data with the correct parameters you can just run a short script like:
          want = 2 % you want parvar1 to be 2
          a = dir(‘*.mat’);
          for n=1:length(a)
          if parvar1==want
          found=1;%you found it
          if found==0 %no file with such parameter
          error(‘Did not find a file with parvar 1 = %g’,want);
          load(a(n).name);%load everything

          That script may not run perfectly as I haven’t ran it. But the concept should work. Then you could save the file names using a convention such as timestamps. Anyway good luck with it.

  4. Dear Julian,

    Thank you so much for your share. It’s a pain for me to sync my Matlab files on my mac and desktop . And your method definitely save my life:)


Leave a Reply

Your email address will not be published. Required fields are marked *