uigetfile with default filter

조회 수: 21 (최근 30일)
Csaba
Csaba 2019년 3월 27일
댓글: Yair Altman 2019년 4월 5일
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??

채택된 답변

Yair Altman
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
Csaba
Csaba 2019년 3월 28일
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.
Yair Altman
Yair Altman 2019년 4월 5일
@Csaba - if you found my answer useful, then please accept it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
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
Csaba
Csaba 2019년 3월 28일
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
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'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by