uigetfile with default filter
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Yair Altman
2019-3-28
编辑:Yair Altman
2019-3-28
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 个评论
更多回答(1 个)
Adam Danz
2019-3-27
编辑:Adam Danz
2019-3-28
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 个评论
Adam Danz
2019-3-28
编辑:Adam Danz
2019-3-28
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'
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!