Pau*_*aul 6 matlab user-interface
所以我对MATLAB目录选择gui有疑问.我需要使用GUI来选择目录,但问题是uigetdir接口很糟糕.如果我这样打电话:
blah = uigetfile('C:\...\T2 Measurements');
Run Code Online (Sandbox Code Playgroud)
这就是它告诉我的:

如你所见,这太糟糕了.有大量关于文件系统中文件位置的无关信息,相关信息都在下面.理想情况下,我想指定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
Run Code Online (Sandbox Code Playgroud)
呸.
您可以通过直接调用Java Swing对象(包括JFileChooser)来绕过uigetdir()并编写自己的小文件选择器函数.这可能是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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8662 次 |
| 最近记录: |