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:
- I used a
com.mathworks.hg.util.dFileChoosercomponent for the main file selector. This is a builtin Matlab control that extends the standard
javax.swing.JFileChooserwith a few properties and methods. I don’t really need the extra features, so you can safely replace the component with a
JFileChooserif 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.
- I used the javacomponent function to place the file-selector component within the dialog window.
- 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).
- 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.
- 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.