MATLAB Function Reference | ![]() ![]() |
Interactively retrieve a filename
Syntax
uigetfile uigetfile('FilterSpec
') uigetfile('FilterSpec
','DialogTitle
') uigetfile('FilterSpec
','DialogTitle
',x,y) [FileName,PathName] = uigetfile(...) [FileName,PathName,FilterIndex] = uigetfile(...)
Description
uigetfile
displays a dialog box used to retrieve a file. The dialog box lists the files and directories in the current directory.
uigetfile('
displays a dialog box that lists files in the current directory. FilterSpec
')
FilterSpec
determines the initial display of files and can be a full filename or include the *
wildcard. For example, '
*.m'
lists all the MATLAB M-files. If FilterSpec
is a cell array, the first column is use as the list of extensions, and the second column is used as the list of descriptions.
uigetfile('
displays a dialog box that has the title FilterSpec
','DialogTitle
')
DialogTitle
.
uigetfile('
positions the dialog box at position [FilterSpec
','DialogTitle
',x,y)
x
,y
], where x
and y
are the distance in pixel units from the left and top edges of the screen. Note that some platforms may not support dialog box placement.
[FileName,PathName] = uigetfile(...)
returns the name and path of the file selected in the dialog box. After you press the Done button, FileName
contains the name of the file selected and PathName
contains the name of the path selected. If you press the Cancel button or if an error occurs, FileName
and PathName
are set to 0
.
[FileName,PathName,FilterIndex] = uigetfile(...)
returns the index of the filter selected in the dialog box. The indexing starts at 1. If the user clicks the Cancel button, closes the dialog window, or if an error occurs, FilterIndex
is se to 0
.
Remarks
If you select a file that does not exist, an error dialog appears. You can then enter another filename, or press the Cancel button.
Examples
This statement displays a dialog box that enables you to retrieve a file. The statement lists all MATLAB M-files within a selected directory. The name and path of the selected file are returned in FileName
and PathName
. Note that uigetfile
appends All Files(*.*)
to the file types when FilterSpec
is a string.
Use a cell array to specify a list of extensions and descriptions:
[filename, pathname] = uigetfile( ... {'*.m;*.fig;*.mat;*.mdl','MATLAB Files (*.m,*.fig,*.mat,*.mdl)'; '*.m', 'M-files (*.m)'; ... '*.fig','Figures (*.fig)'; ... '*.mat','MAT-files (*.mat)'; ... '*.mdl','Models (*.mdl)'; ... '*.*', 'All Files (*.*)'}, ... 'Pick a file');
Separate multiple extensions with no descriptions with semi-colons.
Associate multiple extensions with one description using the first column in the cell array for the file extensions and the second column as the description:
[filename, pathname] = uigetfile( ... {'*.m;*.fig;*.mat;*.mdl','MATLAB Files (*.m,*.fig,*.mat,*.mdl)'; '*.*', 'All Files (*.*)'}, 'Choose a File');
This code checks for the existence of the file and returns a message about the success or failure of the open operation.
[filename, pathname] = uigetfile('*.m', 'Find an M-file'); if isequal(filename,0)|isequal(pathname,0) disp('File not found') else disp(['File ', pathname, filename, ' found']) end
The exact appearance of the dialog box depends on your windowing system.
See Also
uiputfile
![]() | uigetdir | uiimport | ![]() |