uigetfile with default filter

11 views (last 30 days)
Csaba on 27 Mar 2019
Commented: Yair Altman on 5 Apr 2019
I have a program where I am using uigetfile with several (~7) filters. The users complained about that the file selection window always opens with the first filter as default. They want to open with the last filter used as default.
I could not find any description if this is possible in uigetfile. Is there any workaround??

Accepted Answer

Yair Altman
Yair Altman on 28 Mar 2019
Edited: Yair Altman on 28 Mar 2019
Adam Danz had the right idea, but missed a few key things. The FilterIndex field should indeed have influenced the filter selection, but [presumably due to an internal Matlab bug] it does not get propagated to the internal com.mathworks.mwswing.MJFileChooserPerPlatform Java object. If you set this internal Java peer's FileFilterIndex field, the default filter is changed appropriately. Here's a complete working snippet:
First, set up the FileChooser object in memory (it will not be displayed):
filters = {'*.m'; '*.fig'; '*.doc'};
hFileChooser = matlab.ui.internal.dialog.FileOpenChooser('FileFilter',filters, 'Title','Select your file:');
All of the input parameters to FileOpenChooser are optional - they just need to come in P-V (propertyName,value) pairs. The available parameters are Title, InitialPathName, InitialFileName, FileFilter, UseNativeDialog, MultiSelection (there are also a few other undocumented fields but they are not relevant here).
Next, access the FileOpenChooser's underlying Java peer object using the undocumented Peer property and set its FileFilterIndex field:
hWarn = warning('off','MATLAB:structOnObject');
jFileChooser = struct(hFileChooser).Peer.java;
jFileChooser.setFileFilterIndex(1); % this sets the 2nd index (='*.fig')
Note that Java indexing starts at 0, so index #1 means the 2nd filter (*.fig in this example).
Now display the modal dialog, wait for the user's selection and get the results:
selectedFileName = hFileChooser.FileName; % 'abc.fig' for example
selectedPathName = hFileChooser.PathName; % 'C:\path\to\file' for example
selectedFilterIdx = hFileChooser.FilterIndex; % the eventual filter index used by the user (first=1)
selectionState = hFileChooser.State; % -1=initial; 0=dismissed without selection; 1=file was selected
delete(hFileChooser); % clean-up memory
Yair Altman
Yair Altman on 5 Apr 2019
@Csaba - if you found my answer useful, then please accept it.

Sign in to comment.

More Answers (1)

Adam Danz
Adam Danz on 27 Mar 2019
Edited: Adam Danz on 28 Mar 2019
Just change the order of the filters.
uigetfile({'*.fig'; '*.m'})
% 'fig' appears on top
uigetfile({'*.m'; '*.fig'})
% 'm' appears on top
If the filter list is formed programmatically, use flipud() to reverse its order
filters = {'*.m'; '*.fig'; '*.doc'}; %some dynamically created list
% 'doc' appears on top
or you could put the last option first and keep all others in the same order
filters = {'*.m'; '*.fig'; '*.doc'; '*.log'};
Adam Danz
Adam Danz on 28 Mar 2019
Edited: Adam Danz on 28 Mar 2019
uigetfile() invokes a java method "javaMethodEDT" which is a proprietary built-in function used to create and control the dialog. Its main input is a FileOpenChooser object (for more info, see line below).
help matlab.ui.internal.dialog.FileOpenChooser
This undocumented object has several fields that allow you to control several options in the dialog box. None of them allow you to select the default filter (I tried using the 'FilterIndex' field but it didn't affect the default filter).
obj =
FileOpenChooser with properties:
MultiSelection: 0
UseNativeDialog: 0
FileFilter: {5×1 cell}
InitialFileName: ''
PathName: []
FileName: []
FilterIndex: 0
State: 0
UserSelectedMultipleFolders: 0
InitialPathName: 'C:\Users\me\Documents\MATLAB'
Title: 'Select File to Open'

Sign in to comment.


Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!