开发者

Using uigetfile instead of uigetdir to get directories in Matlab

So I have a question about MATLAB directory selection gui. I need to use a GUI to select a directory, but the problem is that the uigetdir interface is awful. If I call like this:

blah = uigetfile('C:\...\T2 Measurements');

This is what it shows me:

Using uigetfile instead of uigetdir to get directories in Matlab

As you can see, this is awful. There's a ton of extraneous information about the location of the file in the filesystem and the relevant information is all below the fold. Ideally, I'd like to specify that the uigetdir function use the uigetfile GUI, or just pass an argument to uigetfile telling it that I'm looking for a directory, not a single file, since this is what the uigetfile GUI looks like:

Using uigetfile instead of uigetdir to get directories in Matlab

But of course, this requ开发者_开发百科ires that I select a file, not a directory. Obviously the directories are not open, so I suppose I could just have the user select any random file in the folder and I can get the pathname, but is there a better way to do this? In another application, I could imagine that my "select a file in the folder" workaround wouldn't work.

Update

I made some very minor adjustments to Andrew Janke's code to make it take the same arguments as uigetdir(). Here's what I came up with:

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


Yuck.

You can bypass uigetdir() and write your own little file chooser function by directly calling Java Swing objects, including the JFileChooser. Which is probably what uigetfile() is doing under the hood.

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


I have changed this function to be able to select multiple files AND folders at the same time

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


Based on Andrew Janke's answer I created a piece of code which uses the MATLAB dialog and enables multi select for directories:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜