File Exchange Pick of the Week

Start MATLAB in Last Working Directory 4

Posted by Brett Shoelson,

Brett's Pick this week is "Startup MATLAB in Last Working Directory", by Michelle Hirsch.

Several years ago, Loren wrote a blog post encouraging people to share the contents (at least thematically) of their startup files. A lengthy and interesting discussion ensued--it's still worth a read. More recently (last week, in fact!), Sean guest-selected as a Pick-of-the-Week Kevin Bartlett's very useful submission that facilitates the saving of "projects"; I wanted to follow Sean's post with a lighter-weight approach to resuming one's previous coding activities in MATLAB.

When I close MATLAB in the middle of working on a project (or projects), I typically leave relevant editor documents open--whenever it's not inconvenient to do so. That way I can right-click on the tab for an open document and change, relatively painlessly, to its parent directory:

I haven't (yet) implemented Kevin's "projects" approach, but tonight, I changed my long-static startup.m file to incorporate Michelle's "Last Working Directory (LWD)" instructions. Michelle's file contains a small HTML document with instructions--and code snippets--detailing how to modify your startup and finish files to automatically resume working in the directory in which you were last working . It's trivially easy to implement, once you know how, and it saves me the "trouble" of having to manually click to resume my last session.

Also, I'm embarassed to say that, though I've used MATLAB (and 'startup') for nearly 20 years, I never even realized we have a 'finish' file to accompany startup.

As always, I welcome your thoughts and comments. Or leave feedback for Michelle here.


Get the MATLAB code

Published with MATLAB® R2013a

4 CommentsOldest to Newest

Thanks for sharing the finish.m, I had never noticed this function either!

If you have a Simulink license, Simulink Project is really useful to define project startup and shutdown script, and to easily manage source control of the files in your project.

I too like MATLAB to start up in the last working directory, and had such a facility in my startup.m for quite a few years. I was aware of finish.m, but took a different approach. Here is the code snippet from my startup.m (running 2011a in MS Windows):

%% Recover most recent directory if defaulted

if strcmp(userpath,[pwd pathsep]) % latest work takes precendence

fid = fopen(fullfile(prefdir, ‘cwdhistory.m’), ‘r’); % no error handling! should generally work

wd = fgetl(fid); % only read the first line (assuming something

fclose(fid);

if isdir(wd) % this folder may have been deleted since we last ran MATLAB

hl = @(s) sprintf(‘%s‘, s, s); % hyperlink anon function

fprintf(1, ['Switched from userpath %s to %s ' ...

'(last working folder)\n'], hl(cd(wd)), hl(wd));

end

end % otherwise specified folder in launch

%% Dynamic per-project additions (subractions) to path

if exist(fullfile(pwd,’setProjectPath.m’), ‘file’), setProjectPath; end

The if-test against userpath was an important point for me. If you start MATLAB by clicking on an M file or a MAT file and choosing an action such as Run, I want MATLAB to start and remain in the directory containing the file I clicked, NOT the last one I used. Likewise if I click a project-specific shortcut which has a “Start in” folder defined. However, if I just start MATLAB without any other context, I do prefer it to return to my last working directory. The root folder of my userpath contains very little, startup.m and finish.m and one or two other items that must always be on the search path, with MATLAB project folders elsewhere. Note that by avoiding finish.m, this approach can work if MATLAB is terminated abruptly or crashes. The setProjectPath.m can be used to have a kind of project specific startup.

For approximately forever, I have had this in my finish.m:

lastDir = cd;
save(‘/directoryAtLastFinish.mat’,'lastDir’)

and this in my startup.m:

load directoryAtLastFinish.mat
cd(lastDir)
clear lastDir

[This works unless MATLAB crashes, in which case I am taken to the prior "lastDir".]

I actually use set/getpref instead of a MAT file. It goes like this.

startup.m:
try
cd(getpref(‘Startup’, ‘dir’, cd));
catch
end

finish.m:
try
setpref(‘Startup’, ‘dir’, cd);
catch
end

These postings are the author's and don't necessarily represent the opinions of MathWorks.