- Undocumented Matlab - https://undocumentedmatlab.com -

uigetfile/uiputfile customizations

Posted By Yair Altman On November 3, 2016 | 8 Comments

Matlab includes a few built-in file and folder selection dialog windows, namely uigetfile, uiputfile and uigetdir. Unfortunately, these functions are not easily extendable for user-defined functionalities. Over the years, several of my consulting clients have asked me to provide them with versions of these dialog functions that are customized in certain ways. In today’s post I discuss a few of these customizations: a file selector dialog with a preview panel, and automatic folder update as-you-type in the file-name edit box.
It is often useful to have an integrated preview panel to display the contents of a file in a file-selection dialog. Clicking the various files in the tree-view would display a user-defined preview in the panel below, based on the file’s contents. An integrated panel avoids the need to manage multiple figure windows, one for the selector dialog and another for the preview. It also reduces the screen real-estate used by the dialog (also see the related resizing customization [1] below).
I call the end-result uigetfile_with_preview; you can download it from the Matlab File Exchange [2]:

filename = uigetfile_with_preview(filterSpec, prompt, folder, callbackFunction, multiSelectFlag)

uigetfile_with_preview


As you can see from the function signature, the user can specify the file-type filter, prompt and initial folder (quite similar to uigetfile, uiputfile), as well as a custom callback function for updating the preview of a selected file, and a flag to enable selecting multiple files (not just one).
uigetfile_with_preview.m only has ~120 lines of code and plenty of comments, so feel free to download and review the code. It uses the following undocumented aspects:

  1. I used a com.mathworks.hg.util.dFileChooser component for the main file selector. This is a builtin Matlab control that extends the standard javax.swing.JFileChooser with a few properties and methods. I don’t really need the extra features, so you can safely replace the component with a JFileChooser if you wish (lines 54-55). Various properties of the file selector are then set, such as the folder that is initially displayed, the multi-selection flag, the component background color [3], and the data-type filter options.
  2. I used the javacomponent function [4] to place the file-selector component within the dialog window.
  3. I set a callback on the component’s PropertyChangeCallback that is invoked whenever the user interactively selects a new file. This callback clears the preview panel and then calls the user-defined callback function (if available).
  4. I set a callback on the component’s ActionPerformedCallback that is invoked whenever the user closes the figure or clicks the “Open” button. The selected filename(s) is/are then returned to the caller and the dialog window is closed.
  5. I set a callback on the component’s file-name editbox’s KeyTypedCallback that is invoked whenever the user types in the file-name editbox. The callback checks whether the entered text looks like a valid folder path and if so then it automatically updates the displayed folder as-you-type.

If you want to convert the code to a uiputfile variant, add the following code lines before the uiwait in line 111:

hjFileChooser.setShowOverwriteDialog(true);  % default: false (true will display a popup alert if you select an existing file)
hjFileChooser.setDialogType(hjFileChooser.java.SAVE_DIALOG);  % default: OPEN_DIALOG
hjFileChooser.setApproveButtonText('Save');  % or any other string. Default for SAVE_DIALOG: 'Save'
hjFileChooser.setApproveButtonToolTipText('Save file');  % or any other string. Default for SAVE_DIALOG: 'Save selected file'

In memory of my dear father.

Categories: GUI, High risk of breaking in future versions, Java, Undocumented feature


8 Comments (Open | Close)

8 Comments To "uigetfile/uiputfile customizations"

#1 Comment By ODJAPO On March 20, 2017 @ 08:35

Hello,

Is there an easy way to change the button name of a classic uigetfile windows as “set file directory” instead of “Open” ?

Thanks for your answer.

#2 Comment By Yair Altman On March 22, 2017 @ 11:16

@ODJAPO – well, the javax.swing.JFileChooser and com.mathworks.hg.util.dFileChooser classes have a setApproveButtonText() method that you can use for this:

hFig = figure('Name','Select directory', 'NumberTitle','off');
jfs = javaObjectEDT('com.mathworks.hg.util.dFileChooser');  % or: com.mathworks.hg.util.dFileChooser
[hjfs,hContainer] = javacomponent(jfs, [0,0,hFig.Position(3:4)], hFig);
hjfs.setApproveButtonText('Set directory');  % or: set(hjfs,'ApproveButtonText','Set directory')

You can edit my uigetfile_with_preview.m utility (which is referenced in the main article text above) accordingly. I do not advise modifying Matlab’s builtin uigetdir function directly.

#3 Comment By Jafa On May 4, 2019 @ 21:25

Hello Yair,

thanks a lot for your helpful work!

Regarding uigetfile customizations, I am curious if there is the possibility of having multiple buttons (in my case I am looking for a 3rd button).

Thanks for your Reply!

#4 Comment By Yair Altman On May 21, 2019 @ 12:51

@Jafa – the simplest way to add buttons is to adapt the Matlab source-code of the uigetfile_with_preview utility (around line #95) by adding Matlab uicontrols in the preview panel. Modifying the Java component (JFileChooser) would be more difficult.

#5 Comment By spencer On June 11, 2020 @ 07:07

Thanks, Yair.

I am not familiar with Java, is there any easiest way to modified it for folder selection? thanks

#6 Comment By Yair Altman On June 12, 2020 @ 00:53

Folder selection can be done using the uigetdir function

#7 Comment By spencer On June 12, 2020 @ 01:34

Thanks for your reply, Yair.

I am trying to find a workaround to replace uigetdir. Below setting in the code can help to select folder and file together.
hjFileChooser.setFileSelectionMode(2);

#8 Comment By James On October 30, 2020 @ 23:01

Is there a way to change the location of the window? They pop up at random locations and the user has to chase them down.


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/uigetfile-uiputfile-customizations

URLs in this post:

[1] resizing customization: #resizable

[2] download it from the Matlab File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/60074-uigetfile-with-preview-gui-dialog-window-with-a-preview-panel

[3] component background color: http://undocumentedmatlab.com/blog/javacomponent-background-color

[4] javacomponent function: http://undocumentedmatlab.com/blog/javacomponent

[5] Figure window customizations : https://undocumentedmatlab.com/articles/figure-window-customizations

[6] Uitab customizations : https://undocumentedmatlab.com/articles/uitab-customizations

[7] Figure toolbar customizations : https://undocumentedmatlab.com/articles/figure-toolbar-customizations

[8] Bar plot customizations : https://undocumentedmatlab.com/articles/bar-plot-customizations

[9] Matlab toolstrip – part 9 (popup figures) : https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures

[10] Font selection components : https://undocumentedmatlab.com/articles/font-selection-components

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.