我们可以使用Java组件,特别是
JFileChooser,并确保我们为它提供了FILES_AND_DIRECTORIES选择标志.
%% Select entity:
jFC = javax.swing.JFileChooser(pwd);
jFC.setFileSelectionMode(jFC.FILES_AND_DIRECTORIES);
returnVal = jFC.showOpenDialog([]);
switch returnVal
case jFC.APPROVE_OPTION
fName = string(jFC.getSelectedFile());
case jFC.CANCEL_OPTION
% do something with cancel
case jFC.ERROR_OPTION
% do something with error
otherwise
throw(MException("fileFolderChooser:unsupportedResult", ...
"Unsupported result returned from JFileChooser: " + returnVal + ...
". Please consult the documentation of the current Java version (" + ...
string(java.lang.System.getProperty("java.version")) + ")."));
end
%% Process selection:
switch true % < this is just some trick to avoid if/elseif
case isfolder(fName)
% Do something with folder
case isfile(fName)
% Do something with file
otherwise
throw(MException('fileFolderChooser:invalidSelection',...
'Invalid selection, cannot proceed!'));
end
这产生了一个熟悉的对话框,如下所示,它完全按预期工作:
在R2018a上测试,使用Java 1.8.0_144(java.lang.System.getProperty(“java.version”)的输出).