【问题标题】:Using uigetfile instead of uigetdir to get directories in MatlabMatlab中使用uigetfile代替uigetdir获取目录
【发布时间】:2011-09-15 00:26:39
【问题描述】:

所以我有一个关于 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

【问题讨论】:

  • @user1740374 您无法通过编辑问题发表评论。当您获得足够的声望时,您可以发表评论。

标签: user-interface matlab


【解决方案1】:

呸。

您可以绕过 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

【讨论】:

    【解决方案2】:

    我已将此功能更改为能够同时选择多个文件和文件夹

    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
    

    【讨论】:

    • 您的代码不适用于非空的第一个参数,请尝试uigetfilesanddirs('C:\')。您的startpath == 比较返回数组,这不是您想要的!
    • 为我工作。简单而伟大!
    【解决方案3】:

    基于 Andrew Janke's answer 我创建了一段代码,它使用 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
    

    【讨论】:

    • 在 Ubuntu 18.04 上为我工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2014-07-08
    • 2012-03-28
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多