首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用uigetfile代替uigetdir在Matlab中获取目录

用uigetfile代替uigetdir在Matlab中获取目录
EN

Stack Overflow用户
提问于 2011-06-14 20:04:38
回答 3查看 10K关注 0票数 6

所以我有一个关于MATLAB目录选择指南的问题。我需要使用GUI来选择目录,但问题是uigetdir界面非常糟糕。如果我这样打电话:

代码语言:javascript
运行
复制
blah = uigetfile('C:\...\T2 Measurements');

这就是它给我的启示:

如你所见,这太可怕了。关于文件系统中文件的位置有大量无关的信息,相关信息都在折叠下面。理想情况下,我希望指定uigetdir函数使用uigetfile GUI,或者将一个参数传递给uigetfile,告诉它我正在寻找一个目录,而不是一个文件,因为uigetfile GUI是这样的:

但当然,这要求我选择一个文件,而不是目录。显然目录没有打开,所以我想我可以让用户选择文件夹中的任意随机文件,我可以获得路径名,但是有更好的方法吗?在另一个应用程序中,我可以想象我的“在文件夹中选择一个文件”解决方案将无法工作。

更新

我对Andrew的代码做了一些非常小的调整,使其具有与uigetdir()相同的参数。我想出的是:

代码语言:javascript
运行
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-14 20:37:38

糟透了。

您可以绕过uigetdir(),直接调用Java对象,包括JFileChooser,编写自己的小文件选择器函数。这可能就是uigetfile()在引擎盖下所做的事情。

代码语言:javascript
运行
复制
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
票数 9
EN

Stack Overflow用户

发布于 2011-08-17 15:22:21

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

代码语言:javascript
运行
复制
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
票数 4
EN

Stack Overflow用户

发布于 2016-09-19 06:23:14

基于Andrew Janke's answer,我创建了一段使用MATLAB对话框的代码,并为目录启用了多个选择:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6349410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档