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

33 views (last 30 days)
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
Geoff Hayes
Geoff Hayes on 27 Jun 2015
Based on your tags, I had assumed that you had created a GUI (perhaps with GUIDE). If this is the case or even if you have created a programmatic GUI then you should be able to create a variable/field within the handles structure or within the script respectively (and so not have to "pollute" the base workspace).
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 (3)

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

NIHAD
NIHAD on 9 Oct 2022
download the attach .mlapp file which can be opened using Matlab2016b and higher version. Or follow the following three steps
Step 1: Add a property to variable to store and share data among callbacks
properties (Access = public)
seleted_folder % Description
end
Step 2: Initilize the variable in step 1 using startup function (you can leave it empty or set a default directory)
function startupFcn(app)
app.seleted_folder=''; % or app.seleted_folder='C:\Program Files';
end
Step 3: Browse button callback
function browseButtonPushed(app, event)
app.seleted_folder=uigetdir(app.seleted_folder);
drawnow;
figure(app.UIFigure)
if ~isequal(app.seleted_folder,0)
app.FolderdirectoryLabel.Text=app.seleted_folder;
else
app.seleted_folder=app.FolderdirectoryLabel.Text;
end
end

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!