Is there a way to tell uigetfile to start browsing in a folder other than the current one?

53 views (last 30 days)
Sometimes I run uigetfile multiple times and the user will typically want to read in files from the same directory/folder each time. I'm looking for a way to tell uigetfile to resume browsing from the last folder visited in the previous call to uigetfile. I can easily obtain the path name of the last folder visited, but can't see how to tell uigetfile to resume from there, other than to CD to the folder, which is awkward for various reasons, and not the solution I prefer.
I'd like to be able to do similar things with uiputfile, etc... Any suggestions?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 25 Jul 2013
Edited: Azzi Abdelmalek on 25 Jul 2013
Edit
uigetfile('*.m','select the m file','E:/matlab')
  3 Comments
Matt J
Matt J on 3 Nov 2021
@David Borrego Not sure what you mean by the ".m folder domain". There is no limitation on the folder you specify in the 3rd argument to uigetfile().

Sign in to comment.

More Answers (1)

Jan
Jan on 25 Jul 2013
CD is not really awkward in my opinion:
persistent lastFolder
if isempty(lastFolder)
lastFolder = cd;
end
bakCD = cd;
cd(lastFolder);
[file, folder] = uigetfile('*.*', 'Choose a file');
cd(bakCD);
if ischar(folder)
lastFolder = folder;
end
...
This opens automatically the last used folder, which is an intuitive behavior. Of course this would work with the method suggested by Azzi also.
  1 Comment
Matt J
Matt J on 25 Jul 2013
Edited: Matt J on 26 Jul 2013
I guess it's not so terrible. It get's a little awkward IMO when you are calling uigetfile through a wrapper instead of calling it directly. Then you either have to pass lastFolder along to the wrapper with an extra argument, as below, or do extra work to extract it from arg1,arg2, etc...
[file, folder] = wrapper(lastFolder,arg1,arg2,...);
function [file, folder] = wrapper(lastFolder, varargin)
backCD=cd;
cd(lastFolder);
[file, folder] = uigetfile(varargin{:});
cd(backCD);
end

Sign in to comment.

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!