MATLAB: How to specify the directory path when I use the UIGETFILE or UIPUTFILE functions in MATLAB 6.5 (R13)

directoryforMATLABpathSpecificationuigetfileuiputfile

I want to be able to specify a directory path for the UIGETFILE or UIPUTFILE functions, such that they open in a specified directory.

Best Answer

It is possible to specify the path to the directory you would like the UIGETFILE or UIPUTFILE functions to begin in, by specifying the path in the FilterSpec input argument. To do this, use the full path to the directory.
Under Macintosh OS X and UNIX, '~' is allowed as a directory specification (current user's home directory). The following example code will list files with a .M extension in the "matlab" subdirectory.
[FileName,PathName] = uigetfile('~/matlab/*.m', 'MATLAB program files')
Note that you cannot use '.' or '..' or directory specifications such as "matlab/*.m", because the
notion of the 'current directory' is not preserved between calls to UIGETFILE or UIPUTFILE.
Care must be taken when specifying more than one file type and their respective description through the use of the cell array notation that is described in the documentation. When using the cell array notation, the path name must be included after the DialogTitle input argument. The following code is an example of how to accomplish this task under the UNIX system:
[FileName,PathName] = uigetfile({'*.dat;*.csv','Data Files (*.dat,*.csv)';'*.*','All Files (*.*)'}, ...
'Pick Another File','/home/');
In this example, the UIGETFILE dialog box with the title, "Pick Another File", will begin in the "home" directory and will list the *.dat and *.csv files.
Under Windows, you can specify the directory in the same manner as described above except using the DOS directory structure notation. The following code is an example of using two calls to UIGETFILE. The first call starts UIGETFILE in the current working directory and lets the user search for a desired file. The second call allows the user to begin in the desired directory that was chosen from the first call.
[FileName,PathName] = uigetfile({'*.dat;*.csv','Data Files (*.dat,*.csv)';'*.*','All Files (*.*)'}, ...
'Pick a File');
[FileName,PathName] = uigetfile({'*.dat;*.csv','Data Files (*.dat,*.csv)';'*.*','All Files (*.*)'}, ...
'Pick Another File',PathName);
The following code is an example of specifying the directory path without using the cell array format.
[FileName PathName] = uigetfile('D:\Applications\Matlab6p5\work\*.m;*.mdl;*.mat','MATLAB Files');
Note that all the above examples behave the same for UIPUTFILE as well.