uigetfile with custom file filter

조회 수: 11 (최근 30일)
Alaster Meehan
Alaster Meehan 2024년 8월 22일
댓글: Alaster Meehan 2024년 8월 26일
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.

답변 (2개)

Shubham
Shubham 2024년 8월 22일
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
  1. Pre-filter the Files: Use your existing function to filter the files in a directory before invoking uigetfile.
  2. Temporary Directory or File List: Create a temporary directory or a list of symbolic links that contain only the filtered files.
  3. Use uigetfile: Open uigetfile on this temporary directory or with the list of filtered files.
% 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
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.
  댓글 수: 1
Alaster Meehan
Alaster Meehan 2024년 8월 26일
Prefiltering the files and copying into a temp directory is not a possible solution as I would not know which directory the user is going to explore. Also these files are also typically several GB so copying would be slow.
At this stage a custom GUI seems to be my only option.

댓글을 달려면 로그인하십시오.


Karanjot
Karanjot 2024년 8월 22일
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
  댓글 수: 1
Alaster Meehan
Alaster Meehan 2024년 8월 26일
This is not really a possible option as I don't know in advance which directory the user will be navigating to.
To copy the files symbolic links would need to be used as the files are several GB in size.
It is difficult to write a custom file explorer that has the full functionality of a windows based file explorer, where the user can navigate between drives and folders, search, cut and paste directories etc, as I said in my question, this becomes a difficult solution.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Files and Folders에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by