- Pre-filter the Files: Use your existing function to filter the files in a directory before invoking uigetfile.
- Temporary Directory or File List: Create a temporary directory or a list of symbolic links that contain only the filtered files.
- Use uigetfile: Open uigetfile on this temporary directory or with the list of filtered files.
uigetfile with custom file filter
12 views (last 30 days)
Show older comments
Is it possible to use a custom function to filter files using uigetfile. Ideally it would be good to be able to specify a function handle of something like that.
Some Background.
I have a bunch of database files that where each database may be spread over several files. There can be several database files in each folder. Ideally it would be nice if I could just show the first file for each database. I already have a function that will return a list of the files I want to display for a given folder.
I could write my own custom file picker but this involves significiantly more work and would not have all functioanlity and niceness of uigetfile.
Is it possible to get a handle to the file picker itself, if so it may be possible to interact with this on the fly.
0 Comments
Answers (2)
Shubham
on 22 Aug 2024
Hi Alaster,
The uigetfile function in MATLAB is a built-in dialog that allows users to select files through a graphical user interface. However, it does not natively support filtering files using a custom function or function handle directly. The filtering options are limited to specifying file extensions or wildcard patterns.
Given your requirements, you have a few options:
1: Pre-filter Files and Use uigetfile
% Assume getFilteredFiles is your custom function that returns filtered files
folderPath = 'your_directory_path';
filteredFiles = getFilteredFiles(folderPath);
% Create a temporary directory or use MATLAB to simulate a file list
% For simplicity, let's assume we simulate a file list
if ~isempty(filteredFiles)
[file, path] = uigetfile(filteredFiles, 'Select a File');
if isequal(file, 0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path, file)]);
end
else
disp('No files to display');
end
You can refer to this documentation: Open file selection dialog box - MATLAB uigetfile (mathworks.com)
2: Custom File Picker
While creating a custom file picker involves more work, it offers full control over the user interface and can directly integrate with your filtering logic. You can use MATLAB's GUI capabilities, such as uifigure, uilistbox, and callback functions to mimic a file picker.
Important Points:
While uigetfile does not support custom function filtering directly, pre-filtering files before invoking it or creating a custom GUI are practical workarounds. If you need the full flexibility of custom filtering and interaction, consider developing a custom file picker using MATLAB's GUI components.
Karanjot
on 22 Aug 2024
Hi Alaster,
The function uigetfile does not support custom filtering functions in MATLAB directly via function handle. It is designed to use standard file filters based on file extensions, which limits its ability to dynamically filter files based on custom logic.
A potential workaround is as follows:
Pre-Filter Files Before Calling uigetfile:
- Use your existing function to filter the files in the directory first. Then, copy or create symbolic links to these files in a temporary directory. Use uigetfile to display files from this temporary directory.
% Assume your function returns a list of files to show
filteredFiles = myCustomFileFilterFunction('path/to/directory');
% Create a temporary directory
tempDir = fullfile(tempdir, 'filteredFiles');
if ~exist(tempDir, 'dir')
mkdir(tempDir);
end
% Copy or link the filtered files
for i = 1:length(filteredFiles)
copyfile(filteredFiles{i}, tempDir);
end
% Use uigetfile to display files from the temporary directory
[file, path] = uigetfile(fullfile(tempDir, '*.*'), 'Select a File');
% Clean up temporary directory after selection
rmdir(tempDir, 's');
To know more, Refer to the following documentation
See Also
Categories
Find more on Files and Folders in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!