UIGETDIR2 Standard dialog box for selecting a directory which remembers last selected directory. UIGETDIR2 is a wrapper for Matlab's UIGETDIR function which adds the ability to remember the last selected directory. UIGETDIR2 stores information about the last selected directory in a mat file which it looks for when called. UIGETDIR2 can only remember the selected directory if the current directory is writable so that a mat file can be stored. Only successful directory selections update the folder remembered. If the user cancels the directory selection dialog box then the remembered path is left the same. Usage is the same as UIGETDIR. uigetdir('','Dialog box text') Empty start path will invoke use of last used directory if dialog text is desired See also UIGETDIR, UIGETFILE, UIPUTFILE.
0001 function directory_name = uigetdir2(varargin) 0002 0003 %UIGETDIR2 Standard dialog box for selecting a directory which remembers 0004 %last selected directory. 0005 % UIGETDIR2 is a wrapper for Matlab's UIGETDIR function which adds the 0006 % ability to remember the last selected directory. UIGETDIR2 stores 0007 % information about the last selected directory in a mat file which it 0008 % looks for when called. 0009 % 0010 % UIGETDIR2 can only remember the selected directory if the current 0011 % directory is writable so that a mat file can be stored. Only 0012 % successful directory selections update the folder remembered. If the 0013 % user cancels the directory selection dialog box then the remembered 0014 % path is left the same. 0015 % 0016 % Usage is the same as UIGETDIR. 0017 % 0018 % uigetdir('','Dialog box text') Empty start path will invoke use of 0019 % last used directory if dialog text is 0020 % desired 0021 % 0022 % 0023 % See also UIGETDIR, UIGETFILE, UIPUTFILE. 0024 0025 % Written by Chris J Cannell 0026 % Contact ccannell@gmail.com for questions or comments. 0027 % 01/05/2006 Created 0028 % 04/18/2007 Script checks if an empty string is passed as the first 0029 % argument, if so then last used directory is used in its 0030 % place (thanks to Jonathan Erickson for the bug report) 0031 0032 0033 % name of mat file to save last used directory information 0034 lastDirMat = 'lastUsedDir.mat'; 0035 0036 0037 %% Check passed arguments and load saved directory 0038 % if start path is specified and not empty call uigetdir with arguments 0039 % from user, if no arguments are passed or start path is empty load saved 0040 % directory from mat file 0041 if nargin > 0 && ~isempty(varargin{1}) 0042 % call uigetdir with arguments passed from uigetdir2 function 0043 directory_name = uigetdir(varargin{:}); 0044 else 0045 % set default dialog open directory to the present working directory 0046 lastDir = pwd; 0047 % load last data directory 0048 if exist(lastDirMat, 'file') ~= 0 0049 % lastDirMat mat file exists, load it 0050 load('-mat', lastDirMat) 0051 % check if lastDataDir variable exists and contains a valid path 0052 if (exist('lastUsedDir', 'var') == 1) && ... 0053 (exist(lastUsedDir, 'dir') == 7) 0054 % set default dialog open directory 0055 lastDir = lastUsedDir; 0056 end 0057 end 0058 0059 % check if second argument (dialog box text) was passed 0060 if nargin == 2 0061 % call uigetdir with saved directory and second argument from user 0062 directory_name = uigetdir(lastDir,varargin{2}); 0063 else 0064 % call uigetdir with saved directory 0065 directory_name = uigetdir(lastDir); 0066 end 0067 end 0068 0069 0070 %% Store last used directory 0071 % if the user did not cancel the directory dialog then update 0072 % lastDirMat mat file with the folder selected 0073 if ~isequal(directory_name,0) 0074 try 0075 % save last folder used to lastDirMat mat file 0076 lastUsedDir = directory_name; 0077 save(lastDirMat, 'lastUsedDir'); 0078 catch 0079 % error saving lastDirMat mat file, display warning, the folder 0080 % will not be remembered 0081 disp(['Warning: Could not save file ''', lastDirMat, '''']); 0082 end 0083 end