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