Open file selection dialog box
opens a modal
dialog box that lists files in the current folder. It enables a user to select or
enter the name of a file. If the file exists and is valid,
file
= uigetfileuigetfile
returns the file name when the user clicks
Open. If the user clicks Cancel or
the window close button (X), uigetfile
returns
0
.
___ = uigetfile(
specifies a file extension by which files displayed in the dialog box are filtered.
Use this syntax with any of the output argument combinations in the previous
syntaxes.filter
)
Typically, only files with a matching file extension are displayed. On some
platforms, uigetfile
displays files that do not match the
filter, but dims those file names. If the filter is missing or empty,
uigetfile
uses the default list of file types (for example,
all MATLAB® files).
___ = uigetfile(___,'MultiSelect',
specifies whether a user can select multiple files. Set the mode to
mode
)'on'
to enable multifile selection. By default it is set to
'off'
.
Windows® libraries can span multiple folders.
Note
The visual characteristics of the dialog box depend on the operating system that runs your code. For instance, some operating systems do not show title bars on dialog boxes. If you pass a dialog box title to the uigetfile function, those operating systems do not display the title.
Display the full file specification of the file selected in
the dialog box. Use the disp
and
fullfile
functions to add explanatory text and
concatenate the path
and file
output
values.
[file,path] = uigetfile('*.m'); if isequal(file,0) disp('User selected Cancel'); else disp(['User selected ', fullfile(path,file)]); end
User selected H:\Documents\MyCode\surf.m
Display the filter index selection with explanatory text in
the Command Window. Use the num2str
function to convert the
numeric filter index value (indx
) to a character array.
Doing so makes the value valid input to the disp
function.
[file,path,indx] = uigetfile; if isequal(file,0) disp('User selected Cancel') else disp(['User selected ', fullfile(path, file),... ' and filter index: ', num2str(indx)]) end
User selected H:\Documents\MyCode\peaks.fig and filter index:
3
Display only files with a .m
extension in
the dialog box by specifying '*. m'
as the
filter
input argument.
[file,path] = uigetfile('*.m');
Create a list of file extensions in the file filter drop-down
list. Pass the filter
input argument as a cell array of
character vectors and separate the file extensions with semicolons.
[file,path] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},... 'File Selector');
Create a list of file extensions and give them descriptions
by passing the filter
input argument as a cell array of
character vectors. The first column of the cell array contains the file
extensions, and the second contains custom descriptions of the file types. This
example also associates multiple file types with the 'MATLAB
Files'
and 'Models'
descriptions.
[file,path,indx] = uigetfile( ... {'*.m;*.mlx;*.fig;*.mat;*.slx;*.mdl',... 'MATLAB Files (*.m,*.mlx,*.fig,*.mat,*.slx,*.mdl)'; '*.m;*.mlx','Code files (*.m,*.mlx)'; ... '*.fig','Figures (*.fig)'; ... '*.mat','MAT-files (*.mat)'; ... '*.mdl;*.slx','Models (*.slx, *.mdl)'; ... '*.*', 'All Files (*.*)'}, ... 'Select a File');
To display a default file name in the File
name field when the dialog box opens, pass the file name as the
defname
input argument
[file,path] = uigetfile('*.png',... 'Select an icon file','icon.png')
To display a default path and file name in the File
name field when the dialog box opens, pass the full file name
as the defname
input argument.
[file,path] = uigetfile('C:\Documents\Work\icon.png',... 'Select an Image File')
Enable multifile selection by setting the
'Multiselect'
option to 'on'. Users can select multiple
files by holding down the Shift or Ctrl key
and clicking file names.
[file,path] = uigetfile('*.m',... 'Select One or More Files', ... 'MultiSelect', 'on');
Use the path and file name that uigetfile
returns to
open, read, or analyze the file using various input and output functions in
MATLAB and MATLAB toolboxes. For example: listed here.
imread
for reading
images.
xlsread
for reading
Microsoft Excel files.
open
, edit
, or run
with MATLAB code files. For example,
this code creates a dialog box to get a MATLAB code file name from the
user, builds a full file name from the returned values, and then
runs the user-specified code file.
[file,path] = uigetfile('*.m'); selectedfile = fullfile(path,file); run(selectedfile);
Use the dir
function to return a filtered or
unfiltered list of files in your current folder or a folder you specify. The
dir
function can return file attributes too.