uigetfile with default filter
Show older comments
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
More Answers (1)
Just change the order of the filters.
Example:
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
uigetfile(flipud(filters))
% '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'};
uigetfile(filters([end,1:end-1]))
4 Comments
I don't know of any matlab setting that affects the order of the filter settings in uigetfile(). You'd need to acces the source code to make changes.
If your list of filters is dynamic, you could just use flipud() to reverse their order.
filters = {'*.m'; '*.fig'; '*.doc'}; %some dynamically created list
uigetfile(flipud(filters))
% 'doc' appears on top
I updated my answer to include this example, too.
Csaba
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'
Categories
Find more on App Building 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!