Main Content

uigetfile

Open file selection dialog box

Description

file= uigetfileopens amodaldialog 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,uigetfilereturns the file name when the user clicksOpen. If the user clicksCancelor the window close button (X),uigetfilereturns0.

[file,path] = uigetfilereturns the file name and path to the file when the user clicksOpen. If the user clicksCancelor the window close button (X), thenuigetfilereturns0for both of the output arguments.

example

[file,path,indx] = uigetfilereturns the index of the filter selected in the dialog box when the user clicksOpen.

example

___= uigetfile(filter)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.

Typically, only files with a matching file extension are displayed. On some platforms,uigetfiledisplays files that do not match the filter, but dims those file names. If the filter is missing or empty,uigetfileuses the default list of file types (for example, all MATLAB®files).

example

___= uigetfile(filter,title)specifies a dialog box title. To filter using the default file filter, but specify a custom title, use empty quotes for the filter value. For example:

file = uigetfile('','Select a File')

example

___= uigetfile(filter,title,defname)specifies a default file name for theFile namefield or a default folder that the dialog opens to.

example

___= uigetfile(___,'MultiSelect',mode)specifies whether a user can select multiple files. Set the mode to'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.

Examples

collapse all

Display the full file specification of the file selected in the dialog box. Use thedispandfullfilefunctions to add explanatory text and concatenate thepathandfileoutput values.

[file,path] = uigetfile('*.m');ifisequal(file,0) disp('User selected Cancel');elsedisp(['User selected ', fullfile(path,file)]);end

文件选择对话框。The visible files are .m files and the file filter drop-down reads (*.m).

User selected H:\Documents\MyCode\surf.m

Display the filter index selection with explanatory text in the Command Window. Use thenum2strfunction to convert the numeric filter index value (indx) to a character array. Doing so makes the value valid input to thedispfunction.

[file,path,indx] = uigetfile;ifisequal(file,0) disp('User selected Cancel')elsedisp(['User selected ', fullfile(path, file),...' and filter index: ', num2str(indx)])end

文件选择对话框。对话框中显示的文件with various extensions, and the file filter drop-down contains the extensions types.

User selected H:\Documents\MyCode\peaks.fig and filter index: 3

Display only files with a.mextension in the dialog box by specifying'*. m'as thefilterinput argument.

[file,path] = uigetfile('*.m');

文件选择对话框。The visible files are .m files and the file filter drop-down reads (*.m).

Create a list of file extensions in the file filter drop-down list. Pass thefilterinput argument as a cell array of character vectors and separate the file extensions with semicolons.

[file,path] = uigetfile({'*.m';'*.slx';'*.mat';'*.*'},...'File Selector');

文件选择对话框。The file filter drop-down has an option for each specified file extension, and (*.m) is selected. The visible files are .m files.

Create a list of file extensions and give them descriptions by passing thefilterinput 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');

文件选择对话框。The file filter drop-down lists the specified file filter descriptions. The visible files are .m and .fig files.

To display a default file name in theFile namefield when the dialog box opens, pass the file name as thedefnameinput argument

[file,path] = uigetfile('*.png',...'Select an icon file','icon.png')

文件选择对话框。The title of the dialog box is Select an icon file and the default file name is icon.png. The visible files are .png files.

To display a default path and file name in theFile namefield when the dialog box opens, pass the full file name as thefilterinput argument.

[file,path] = uigetfile('C:\Documents\Work\icon.png',...'Select an Image File')

文件选择对话框。The title of the dialog box is Select an Image File. The dialog is open to the C:\Documents\Work folder and the default file name is icon.png. The visible files are .png files.

Create a list of file extensions by passing thefilterinput argument as a cell array of character vectors. Specify the folder that the dialog box opens to.

[file,path] = uigetfile({'*.png';'*.m'},...'Select a file','C:\Documents\AppBuildingFiles\')

文件选择对话框。The title of the dialog box is Select a file. The dialog is open to the C:\Documents\AppBuildingFiles folder and the file filter drop-down contains a .png and a .m filter.

Enable multifile selection by setting the'Multiselect'option to'on'. Users can select multiple files by holding down theShiftorCtrlkey and clicking file names.

[file,path] = uigetfile('*.m',...'Select One or More Files',...'MultiSelect','on');

文件选择对话框。The title of the dialog box is Select One of More Files. Multiple files in the dialog are selected.

Input Arguments

collapse all

File filter, specified as a character vector, a cell array of character vectors, or a string array. The table below lists the types of inputs you can pass to thefilterargument and the corresponding behavior of the dialog box.

Input Behavior Examples
File name

The file name appears in theFile namefield of the dialog box. The extension of the file is the default filter value.

If the file name contains a path, the dialog box opens to the specified folder. Otherwise, it opens to the current folder. If the specified folder does not exist, thenuigetfileopens the dialog box to the current folder.

  • 'icon.png'

  • 'C:\Documents\icon.png'

  • '..\icon.png'

Folder

The dialog box displays the contents of the folder. TheFile namefield is empty, and no filter applies. To specify a folder name, the last character offiltermust be either a backslash (\) or a slash (/).

If the specified folder does not exist, thenuigetfileopens the dialog box to the current folder.

  • 'C:\Documents\'

File extension filters

The dialog box displays only the files with a matching file extension.

To allow users to choose between multiple filters, specify a cell array or string array with an item for each filter. The filters appear in the filter field drop-down menu in the dialog box.

To create a filter with more than one file extension, specify the extensions within a single character vector or string, separated by a semicolon (;).

  • '*.m'

  • {“* m”;“*打烊lx'}

  • {'*.m;*.mlx';'*.png;*.jpeg'}

File extension filters with descriptions

The dialog box displays the extensions with their descriptions in the filter field. Users can choose between filters.

To display filter descriptions, specify two columns in the cell array or string array. The first column contains a list of file extensions. The second column contains a corresponding list of descriptions. These descriptions replace standard descriptions in the filter field. A description cannot be empty.

  • {'*.m;*.mlx','Code files';'*.png;*.jpeg','Image files'}

If the file filter contains a path, that path can contain these characters:

  • .

  • ..

  • \

  • /

  • ~

For example,'../*.m'lists all code files with a.mextension in the folder above the current folder.

Note

If you or a user includes either an asterisk (*) or a question mark (?) in a file name, thenuigetfiledoes not respond to clickingOpen. The dialog box remains open until the user clicksCancelor removes the wildcard characters from the name. This restriction applies to all platforms, even to file systems that permit these characters in file names.

Dialog box title, specified as a character vector.

Example:'Select a File'

DefaultFile namefield value, specified as a character vector or a string scalar. Thedefnamevalue can specify a path, or a path and a file name.

  • If you specify a path, it can contain the following characters:

    • .

    • ..

    • \

    • /

    • ~

  • To specify a folder name only, make the last character of thedefnamevalue either a backslash (\) or a slash (/).

Example:'myfile.mat'

Example:'C:\Documents\my_MATLAB_files'

Example:'..\myfile.mat'

Example:'..\Documents\'

Multiselect mode, specified as'on'or'off'. If multiselect mode is off, then a user can select one file only. If multiselect mode is on, then a user can select multiple files. If a user selects multiple files, then they must be in the same folder; otherwise MATLAB displays a warning dialog box. Microsoft®Windows libraries can span multiple folders.

Output Arguments

collapse all

File name that the user specified in the dialog box, returned as a character vector or a cell array of character vectors.

A cell array of character vectors is returned when'MultiSelect'is set to'on'and a user selects multiple files. Each array element contains the name of a selected file. File names in the cell array are sorted in the order that the user's platform uses. If a user selects multiple files, they must be in the same folder, otherwise MATLAB displays a warning dialog box.

If the user clicks theCancelbutton or the window close button (X), then MATLAB returns the file value as0.

Path to the specified file or files, returned as a character vector.

If the user clicks theCancelbutton or the window close button (X), then MATLAB returns the file value as0.

Selected filter index, returned as an integer.

The filter is the unlabeled dialog box control to the right of theFile namefield in the dialog box. The filter index value corresponds to the item selected in the filter drop-down list. The index of the first row is 1.

If the user clicks theCancelbutton or the window close button (X), then MATLAB returns an index value of0.

More About

collapse all

Modal Dialog Box

A modal dialog box prevents a user from interacting with other MATLAB windows before responding to the dialog box.

Tips

  • Use the path and file name thatuigetfilereturns to open, read, or analyze the file using various input and output functions in MATLAB and MATLAB toolboxes. For example: listed here.

    • imreadfor reading images.

    • xlsreadfor reading Microsoft Excel files.

    • open,edit, orrunwith 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.

      (文件,路径)= uigetfile(“* m”);selectedfile =富尔语lfile(path,file); run(selectedfile);

Alternative Functionality

Use thedirfunction to return a filtered or unfiltered list of files in your current folder or a folder you specify. Thedirfunction can return file attributes too.

Introduced before R2006a