How to keep/use last used CURRENT DIR?

One decision has proposed by om_s (at russian matlab-forum):
% put this file in matlabroot\toolbox\local\startup.m
cd_path=strcat(prefdir,'\cwdhistory.m');cd_dir=importdata(cd_path);
cd_path=cd_dir{1};cd(cd_path);clear cd_path cd_dir;
But I don't believe that there is no decision by MATLAB IDE way (any radiobutton or...). The goal is: every MATLAB session begins with current folder that have used in last session.

 Akzeptierte Antwort

Daniel Shub
Daniel Shub am 24 Jun. 2011

2 Stimmen

I think in order to be consistent with the -r and -sd (Windows only) startup flags and environment variables on UNIX, it might be better to change userpath in finish.m. This way, if a startup path is specified, it does not get overwritten. I would simply add:
userpath(pwd)
to finish.m. This way also handles the case where the last directory is deleted after quitting MATLAB.

3 Kommentare

Igor
Igor am 24 Jun. 2011
It's better...
(restricted for Windows)
Daniel Shub
Daniel Shub am 24 Jun. 2011
There is no reason why it should be restricted to Windows.
Igor
Igor am 24 Jun. 2011
as I understood help on userpath, for UNIX some additional commands are required

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Bjorn Gustavsson
Bjorn Gustavsson am 24 Jun. 2011

0 Stimmen

Maybe something like this in your finish.m:
last_working_dir = pwd;
save(fullfile('~','.matlab','lastest_dir.mat'),'last_working_dir')
Then in your startup - preferably not in the $matlabroot directory, then all users will get the same behaviour, and as GB Shaw said: "Dont do to others what you want them do to you - they might have different preferences":
if exist(fullfile('~','.matlab','lastest_dir.mat'),2)
load(fullfile('~','.matlab','lastest_dir.mat'))
% Wrap any amunt of finesse around the cd...
cd(last_working_dir)
end
HTH, Bjoern

5 Kommentare

Aurelien Queffurust
Aurelien Queffurust am 24 Jun. 2011
I would have also used your same trick : using both finish.m and startup.m files
Igor
Igor am 24 Jun. 2011
Too complex... if statement.. mat-file usage... 2 files will be written...
My (om_s's) way is rather simple
As I suppose, MATLAB environment way doesn't exist!
Daniel Shub
Daniel Shub am 24 Jun. 2011
What is the advantage of writing the last directory to a file in finish.m, since it is already saved in cwdhistory (as pointed out in the question)?
Igor
Igor am 24 Jun. 2011
yes, yes
I said this om_s when he proposed first to use both files...
Bjorn Gustavsson
Bjorn Gustavsson am 24 Jun. 2011
@Igor, only one file is written, you have startup.m and finish.m anyways.
@Daniel: If done the way I suggest with finish.m you get to the last used working directory of the matlab session most recently closed, what happens with cwdhistory.m when you have more than one matlab session running I couldn't be bothered to find out since it was so easy to make something where I know for sure what's happening.

Melden Sie sich an, um zu kommentieren.

Roberto
Roberto am 31 Dez. 2014

0 Stimmen

There´s a tutorial here:

https://www.youtube.com/watch?v=yz6ZGVOrPUM

Ian
Ian am 10 Sep. 2015
Bearbeitet: Ian am 10 Sep. 2015

0 Stimmen

In Matlab R2015a for Mac OS X, this is a setting one can change in the preferences:
Preferences | Matlab | General
One can select one of three options:
Location based on Matlab Startup Rules
Last working folder from previous Matlab Session
(Specify the full path to a folder ...)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 24 Jun. 2011

Bearbeitet:

Ian
am 10 Sep. 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by