wildcard in filename for readtable does not work

When trying to load the table in matlabwith table = readtable("...\SP17\2022SP17.xlsx"), I can get the file loaded, but when I try to do it with a wildcard: table = readtable("...\SP17\2022*.xlsx") (or another variation with an asterix) it gives the error:
Error using readtable
Unable to find or open '...\SP17\2022*.xlsx'. Check the path and filename or file permissions.
I need to use the wildcard later as I don't know the full name of the files, I need to use.

1 Comment

"when I try to do it with a wildcard: table = readtable("...\SP17\2022*.xlsx") "
Can you point to the exact location in the READTABLE documentation where it states that the filename can include wildcard characters?
"I need to use the wildcard later as I don't know the full name of the files"
Use DIR:

Sign in to comment.

 Accepted Answer

If I understood your question correctly, I would do it this way:
Files = ls('.\SP17\2022*.xlsx')
nFiles = size(Files); nFiles = nFiles(1);
for ii = 1:nFiles
tmpFile = strtrim(Files(ii,:))
table = readtable(tmpFile)
% Do your computation
end
I haven't tested this short code, but anyway the principle should be clear.
Edited after comment from Stephen23.

3 Comments

This code is buggy for due to the rather poor choice of LS rather than the usual DIR, e.g.:
  • LS returns a character array where each row contains one or more file/folder names. So Files(ii) will be the first character of each row, which is very unlikely to be useful or correct.
  • In general file/folder names can have different lengths, but the output from LS is left-aligned and right-padded with spaces to the length of the longest file/folder name. So to handle the names robustly requires not only splitting them from the character array but also stripping those extra trailing space characters. At this point astute readers will notice that space characters are perfectly valid characters within file/folder names (and extensions are optional), so there is literally nothing that can distinguish between trailing space characters added by LS and trailing space characters within the name itself. Stripping them can distort the names so they do not match the actual names. Using LS like this is fundamentally fragile/buggy. There is no way to resolve this... except by easily avoiding the situation and writing better code using DIR.
Using DIR is also what the MATLAB documentation recommends:
It was a pull ahead answer, the code was not tested and as written above it should only show the principle. . I've edited the code should now run w/o error.
Files = ls('../..')
Files =
' bd_build boot Data etc lib MATLAB 'MATLAB Drive' mnt proc root sbin SupportPackages tmp usr bin CourseFiles dev home lib64 'MATLAB Add-Ons' media opt public run srv sys users var '
You can see that on MacOS and Linux, ls returns a character array with many different files on the same line -- unless, that is, you use the -1 option
Files = ls('-1', '../..')
Files =
'bd_build bin boot CourseFiles Data dev etc home lib lib64 MATLAB 'MATLAB Add-Ons' 'MATLAB Drive' media mnt opt proc public root run sbin srv SupportPackages sys tmp users usr var '

Sign in to comment.

More Answers (1)

You must call readtable with a valid filename for an existing file. It will not accept wildcards.
You could use uigetfile to open a dialog that will allow you to pick the file. It will return the file path and name. Then use fullfile to create the full filename, including path, that you can pass to readtable.

1 Comment

The problem with a wildcard is that there is no guarantee it returns a single file. You could use wildcards to collect file names using the dir function or datastores, and then use the result of that to load files.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!