问题描述
||
所以我有一个关于MATLAB目录选择gui的问题。我需要使用GUI来选择目录,但是问题是uigetdir接口很糟糕。如果我这样打电话:
blah = uigetfile(\'C:\\...\\T2 Measurements\');
这就是给我看的:
如您所见,这太糟糕了。关于文件在文件系统中的位置,有大量无关的信息,而所有相关信息都在首位。理想情况下,我想指定uigetdir函数使用uigetfile GUI,或者只是将一个参数传递给uigetfile告诉它我正在寻找目录,而不是单个文件,因为这就是uigetfile GUI的外观喜欢:
但是,当然,这要求我选择一个文件,而不是目录。显然目录没有打开,所以我想我可以让用户选择文件夹中的任何随机文件,然后获得路径名,但是有没有更好的方法呢?在另一个应用程序中,我可以想象我的“选择文件夹中的文件”解决方法不起作用。
更新资料
我对Andrew Janke的代码做了一些非常小的调整,以使其采用与uigetdir()相同的参数。这是我想出的:
function [pathname] = uigetdir2(start_path,dialog_title)
% Pick a directory with the Java widgets instead of uigetdir
import javax.swing.JFileChooser;
if nargin == 0 || start_path == \'\' || start_path == 0 % Allow a null argument.
start_path = pwd;
end
jchooser = javaObjectEDT(\'javax.swing.JFileChooser\',start_path);
jchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if nargin > 1
jchooser.setDialogTitle(dialog_title);
end
status = jchooser.showOpenDialog([]);
if status == JFileChooser.APPROVE_OPTION
jFile = jchooser.getSelectedFile();
pathname = char(jFile.getPath());
elseif status == JFileChooser.CANCEL_OPTION
pathname = [];
else
error(\'Error occured while picking file.\');
end
解决方法
uck
您可以绕过uigetdir()并通过直接调用Java Swing对象(包括JFileChooser)来编写自己的文件选择器功能。 uigetfile()可能是在做什么。
function [file] = pickDirUsingJFileChooser
%PICKDIRUSINGJFILECHOOSER Pick a dir with Java widgets instead of uigetdir
import javax.swing.JFileChooser;
jchooser = javaObjectEDT(\'javax.swing.JFileChooser\');
jchooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
status = jchooser.showOpenDialog([]);
if status == JFileChooser.APPROVE_OPTION
jFile = jchooser.getSelectedFile();
file = char(jFile.getPath());
elseif status == JFileChooser.CANCEL_OPTION
file = [];
else
error(\'Error occurred while picking file\');
end
, 我已更改此功能,以便能够同时选择多个文件和文件夹
function [pathname] = uigetdir2(start_path,dialog_title)
% Pick a directory with the Java widgets instead of uigetdir
import javax.swing.JFileChooser;
if nargin == 0 || start_path == \'\' || start_path == 0 % Allow a null argument.
start_path = pwd;
end
jchooser = javaObjectEDT(\'javax.swing.JFileChooser\',start_path);
jchooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if nargin > 1
jchooser.setDialogTitle(dialog_title);
end
jchooser.setMultiSelectionEnabled(true);
status = jchooser.showOpenDialog([]);
if status == JFileChooser.APPROVE_OPTION
jFile = jchooser.getSelectedFiles();
pathname{size(jFile,1)}=[];
for i=1:size(jFile,1)
pathname{i} = char(jFile(i).getAbsolutePath);
end
elseif status == JFileChooser.CANCEL_OPTION
pathname = [];
else
error(\'Error occured while picking file.\');
end
, 基于Andrew Janke的回答,我创建了一段代码,该代码使用MATLAB对话框并为目录启用了多重选择功能:
function [files] = uigetdirMultiSelect()
import com.mathworks.mwswing.MJFileChooserPerPlatform;
jchooser = javaObjectEDT(\'com.mathworks.mwswing.MJFileChooserPerPlatform\');
jchooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
jchooser.setMultiSelectionEnabled(true);
jchooser.showOpenDialog([]);
if jchooser.getState() == javax.swing.JFileChooser.APPROVE_OPTION
jFiles = jchooser.getSelectedFiles();
files = arrayfun(@(x) char(x.getPath()),jFiles,\'UniformOutput\',false);
elseif jchooser.getState() == javax.swing.JFileChooser.CANCEL_OPTION
files = [];
else
error(\'Error occurred while picking file\');
end