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

조회 수: 33 (최근 30일)
John
John 2015년 6월 27일
답변: NIHAD 2022년 10월 9일
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
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
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
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.

Fabian Torres
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

NIHAD
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by