How can one get uigetdir/uigetfile to remember last opened directory?

48 views (last 30 days)
John
John on 27 Jun 2015
Answered: Fabian Torres on 17 May 2019
Neither uigetdir nor uigetfile remember the last directory selected or the directory of the last file selected. Is there a way to get them to remember last selected directories between invocations?
If there's no built in solution for this, I'll take recommendations for possible workarounds.
Here's my workaround: Save the selected directory path as a matlab workspace variable after each invocation of uigetdir or uigetfile. Then before calling uigetdir or uigetfile, check for the existence of the workspace variable and load it and use that as the initial directory location for uigetdir and uigetfile.
Are there better ways?
  4 Comments
dpb
dpb on 27 Jun 2015
It can, yes, although that has some danger in that you could, despite all good intentions, manage to create your name to overwrite an existing variable or, more likely, another script or unwary user could clear your carefully saved variable.
It's unfortunate TMW didn't provide a full interface to the OS API to allow users to set the directory on open, but that's the problem with proprietary and "easy to use" app's like Matlab; you get what the vendor thinks you need and is willing to go to the trouble to implement, whereas if one uses a lower level language one can get at the APIs directly at the cost of losing all the packaged other tools. "There is no free lunch". I see looking at the .m file that they seem to have gone to the Java side; perhaps it wouldn't be too much trouble to call it directly altho I really don't know Java at all...

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 28 Jun 2015
As a minor modification: you could use setpref() / getpref() if you want to be able to have the information saved between runs.

Fabian Torres
Fabian Torres on 17 May 2019
This worked for me
clearvars -except runPath
if exist('runPath', 'var') == 0 || isempty(runPath)
runPath = 'D:\';
end
runPath = uigetdir(runPath);
if runPath == 0
clearvars
return
end

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by