MATLAB Answers

Csaba
1

uigetfile with default filter

Asked by Csaba
on 27 Mar 2019
Latest activity Commented on by 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??

  0 Comments

Sign in to comment.

2 Answers

Answer by Yair Altman on 28 Mar 2019
Edited by 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;
warning(hWarn);
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:
hFileChooser.show();
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

  3 Comments

Adam Danz
on 28 Mar 2019
Nice! I didn't know about the Java peer object access.
Csaba
on 28 Mar 2019
Thanks, I also started to check and got to the FileOpenChooser. I am affraid of undocumented features since I am distributing the program. Anyway I will try it.
Thanks again.
@Csaba - if you found my answer useful, then please accept it.

Sign in to comment.


Answer by Adam Danz
on 27 Mar 2019
Edited by Adam Danz
on 28 Mar 2019

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

Show 1 older comment
Adam Danz
on 27 Mar 2019
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
I do not want to change order. I just want to select the default, exactly as the user does when selecting in the window.
It sounds for me a reasonable need from the user. Maybe I have to have a look at the source file of uigetfile.
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.