Using uigetfile instead of uigetdir to get directories in matlab

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

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

Here is what he shows me:

uigetdir results

As you can see, this is terrible. There, a ton of extraneous information about the location of the file in the file system and related information is under the palette. Ideally, I would like to indicate that the uigetdir function uses the uigetfile GUI or simply passes an argument to the uiget file telling it that I am looking for a directory and not a single file, because it looks like the uigetfile GUI looks like:

uigetfile results

But, of course, for this you need to select a file, not a directory. The directories are obviously not open, so I suppose I could just pick a random random file in a folder, and I can get the path name, but is there a better way to do this? In another application, I could imagine that my โ€œselect file in folderโ€ method would not work.

Update

I made some very minor adjustments to Andrew Janke code to make it accept the same arguments as uigetdir (). Here is 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 
+6
source share
3 answers

Ugh.

You can get around uigetdir () and write your own little file picker by directly calling Java Swing objects, including JFileChooser. This is most likely what uigetfile () does 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 
+9
source

I 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 
+4
source

Based on, I created a code snippet that uses the MATLAB dialog and includes 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
source

Source: https://habr.com/ru/post/890540/


All Articles