Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Startup in most recent directory

Subject: Startup in most recent directory

From: Andrew

Date: 23 Jul, 2009 16:59:02

Message: 1 of 6

I've searched all over the help, but I cannot find out how I can set MATLAB to startup in the most recent directory (the last current directory). Since it does not seem possible to select this option in the preferences, I imagine I would have to include some script in the startup.m file. I know MATLAB stores the xx most recent directories, but I cannot find out how to access them other than through the directory browser or the toolbar.

Does anybody know of a way that I can accomplish this goal; either by accessing the most recent directory from the list that MATLAB maintains or by some other means?

Any help is appreciated.

Subject: Startup in most recent directory

From: Ashish Uthama

Date: 23 Jul, 2009 17:18:31

Message: 2 of 6

On Thu, 23 Jul 2009 12:59:02 -0400, Andrew
<aarockhi.remove.this@wisc.edu> wrote:

> I've searched all over the help, but I cannot find out how I can set
> MATLAB to startup in the most recent directory (the last current
> directory). Since it does not seem possible to select this option in
> the preferences, I imagine I would have to include some script in the
> startup.m file. I know MATLAB stores the xx most recent directories,
> but I cannot find out how to access them other than through the
> directory browser or the toolbar.
>
> Does anybody know of a way that I can accomplish this goal; either by
> accessing the most recent directory from the list that MATLAB maintains
> or by some other means?
>
> Any help is appreciated.

One approach:

Use finish.m to save the last working directory in a MAT file.
Use startup.m to check for existence of this MAT file and load,cd into it
if present.

Search the doc for 'startup' and 'finish'.

Subject: Startup in most recent directory

From: Bruno Luong

Date: 23 Jul, 2009 17:24:50

Message: 3 of 6

Here is a realization:

% startup.m

fprintf('Have a nice Matlab ride...\n')

% Recover the lastpath saved by finish.m
try
    upath = userpath;
    if ~isempty(upath)
        breakpos = find(upath==';',1,'first');
        if ~isempty(breakpos)
            upath(breakpos:end)=[];
        end
    else
        upath = '';
    end
    s=load([upath filesep 'lastpath.mat']);
    cd(s.lastpath)
end
% bsxops(1);

% finish.m
% Save the lastpath for the next startup
try
    upath = userpath;
    if ~isempty(upath)
        breakpos = find(upath==';',1,'first');
        if ~isempty(breakpos)
            upath(breakpos:end)=[];
        end
    else
        upath = '';
    end
    lastpath = pwd();
    save([upath filesep 'lastpath.mat'], 'lastpath');
end

% Bruno

Subject: Startup in most recent directory

From: Andrew

Date: 27 Jul, 2009 14:21:02

Message: 4 of 6

Thanks to the both of you for your help. I have implemented the suggested code by Bruno and it works well. Thanks again.

Subject: Startup in most recent directory

From: Jan Simon

Date: 27 Jul, 2009 16:15:20

Message: 5 of 6

Dear Andrew!

Another idea is using the already existing list of directories in:
PREFDIR/cwdhistory.m

Example:

% Insert in startup.m:

CWDHist = fullfile(prefdir, 'cwdhistory.m');
CWDList = textread(CWDHist, '%s', 'delimiter', '\n', 'whitespace', '');
if ~isempty(CWDList)
  cd(CWDList{1});
end

%%%

I'm still confused why this file has the extension ".m", but who cares...

Kind regards, Jan

Subject: Startup in most recent directory

From: Andrew

Date: 27 Jul, 2009 17:54:02

Message: 6 of 6

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <h4kjqo$d61$1@fred.mathworks.com>...
> Dear Andrew!
>
> Another idea is using the already existing list of directories in:
> PREFDIR/cwdhistory.m
>
> Example:
>
> % Insert in startup.m:
>
> CWDHist = fullfile(prefdir, 'cwdhistory.m');
> CWDList = textread(CWDHist, '%s', 'delimiter', '\n', 'whitespace', '');
> if ~isempty(CWDList)
> cd(CWDList{1});
> end
>
> %%%
>
> I'm still confused why this file has the extension ".m", but who cares...
>
> Kind regards, Jan

Jan,

Thank you. This was exactly what I was looking for. I didn't know where MATLAB stored the directory list; but now I do thanks to you!

Best regards,
Andy

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us