Monday, May 21, 2007

Start MATLAB in your last working directory

For most MATLAB users, probably, it is more convenient to start MATLAB in the last working directory. There is no option to set in the preferences, an alternative way to do so is using startup.m and finish.m.

When MATLAB starts, it automatically executes startup.m if the m file exists; and it runs a script finish.m when quiting. Therefore, we creat/edit these two files to make sure MATLAB starts in the last working directory.

************************************************
The content of startup.m:
if ispref('StartupDirectory','LastWorkingDirectory')
 lwd = getpref('StartupDirectory','LastWorkingDirectory');
 try
     cd(lwd)
 catch
     disp('Sorry, but I could not go to your last working directory:')
     disp(lwd)
 end
end

***********************************************************
finish.m:
setpref('StartupDirectory','LastWorkingDirectory',pwd)

*************************************************************

Reference: http://www.mathworks.com/matlabcentral/files/5793/StartupInLastDirectoryDirections.html

3 comments:

  1. in windows, if you don't remove the -sd from the target field in the properties of the icon you use to open matlab you will automatically be moved to the directory defined by the Target field. I'm still having trouble getting it to work. oh well.

    ReplyDelete
  2. ah, what might be obvious to most wasn't so obvious to this thick-headed newcomer.

    There is an option in the properties for the icon which is the Start-In field. if this start-in field isn't left blank then you will start in (as goes the name) whatever field you have designated in the Start-in field. no matter what your startup script says.

    ReplyDelete
  3. Hi, David,
    To start MATLAB in the last working directory, you need to creat TWO m files: startup.m and finish.m. The contents are shown in the post. The start-in field doesn't affect the startup scritp.

    ReplyDelete