How can one get uigetdir/uigetfile to remember last opened directory?
36 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff Hayes
2015-6-27
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
2015-6-27
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...
回答(3 个)
Walter Roberson
2015-6-28
As a minor modification: you could use setpref() / getpref() if you want to be able to have the information saved between runs.
0 个评论
Fabian Torres
2019-5-17
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
0 个评论
NIHAD
2022-10-9
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!