Solution to slow execution of "uigetfile" function with an enormous number of files in a folder
Fernando Meo
on 18 Dec 2024
This post is more of a "tips and tricks" guide than a question.
If you have a folder with an enormous number of files and want to use the uigetfile function to select specific files, you may have noticed a significant delay in displaying the file list.
Thanks to the assistance from MathWorks support, an interesting behavior was observed.
For example, if a folder such as Z:\Folder1\Folder2\data contains approximately 2 million files, and you attempt to use uigetfile to access files with a specific extension (e.g., *.ext), the following behavior occurs:
Method 1: This takes minutes to show me the list of all files
[FileName, PathName] = uigetfile('Z:\Folder1\Folder2\data\*.ext', 'File selection');
Method 2: This takes less than a second to display all files.
[FileName, PathName] = uigetfile('*.ext', 'File selection','Z:\Folder1\Folder2\data');
Method 3: This method also takes minutes to display the file list. What is intertesting is that this method is the same as Method 2, except that a file seperator "\" is added at the end of the folder string.
[FileName, PathName] = uigetfile('*.ext', 'File selection','Z:\Folder1\Folder2\data\');
I was informed that the Mathworks development team has been informed of this strange behaviour.
I am using 2023a, but think this should be the same for newer versions.
5 Comments
Indeed, it is a behavior I had previously discovered, albeit not to quite the extent as your example...
What is irritating is that fullfile will build Method 3 for you because of its penchant to return the trailing file separator...
>> filesep
ans =
'\'
>>
At command line, did file completion for folder with "'2<tab>" which returned the forward slash separator even, then added the wildcard file name field..
>> fullfile(FDF,'2020/','*.x*')
ans =
'C:\Foundation\Financial\2020\*.x*'
>>
Does return a fully-qualified name string with system-consistent separators.
But, if stop at the folder level, then
>> fullfile(FDF,'2020/')
ans =
'C:\Foundation\Financial\2020\'
>>
it builds the Method 3 above bottleneck automagically unless one is aware of this penchant and then manually strips the trailing separator.
Most irritating...I generally wrap the fullfile call with
>> strip(fullfile(FDF,'2020/'),filesep)
ans =
'C:\SCCC Foundation\Financial\2020'
>>
to clean up the folder string. I debated aliasing, but haven't yet gone to that extreme.
"This post is more of a "tips and tricks" guide than a question."
Consider posting it here: https://www.mathworks.com/matlabcentral/discussions/tips