uigetfile/uiputfile customizations

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 below).

I call the end-result uigetfile_with_preview; you can download it from the Matlab File Exchange:

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


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, and the data-type filter options.
  2. I used the javacomponent function 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

Tags: , , ,

Bookmark and SharePrint Print

4 Responses to uigetfile/uiputfile customizations

  1. ODJAPO says:


    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.

    • @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.

  2. Jafa says:

    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!

    • @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.

Leave a Reply

Your email address will not be published. Required fields are marked *