Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

uigetdir results

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:

uigetfile results

But of course, this requires 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
like image 728
Paul Avatar asked Jun 14 '11 20:06

Paul


People also ask

What does Uigetfile do in MATLAB?

file = uigetfile opens a modal dialog box that lists files in the current folder. It enables a user to select or enter the name of a file. If the file exists and is valid, uigetfile returns the file name when the user clicks Open. If the user clicks Cancel or the window close button (X), uigetfile returns 0 .

How do I get the current directory in MATLAB?

pwd returns the MATLAB® current folder. currentFolder = pwd returns the path to the current folder.

How do I list only directories in MATLAB?

ls name lists the files and folders in the current folder that match the specified name. list = ls(___) returns the names of all the files and folders in the current folder that match the specified name . You can specify list with any of the arguments in the previous syntaxes.

How do I change the directory in MATLAB?

Change, and then Restore Current Folder Change the current folder to C:\Program Files , saving the folder path before changing it. Use the cd command to display the new current folder. Change the current folder back to the original folder, using the stored path. Use the cd command to display the new current folder.


3 Answers

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
like image 178
Andrew Janke Avatar answered Nov 15 '22 06:11

Andrew Janke


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
like image 24
Peugas Avatar answered Nov 15 '22 05:11

Peugas


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
like image 42
Nils Avatar answered Nov 15 '22 06:11

Nils