wildcard in filename for readtable does not work
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lieke van Boxtel
el 20 de Nov. de 2023
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 comentario
Stephen23
el 20 de Nov. de 2023
Editada: Stephen23
el 20 de Nov. de 2023
"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:
Respuesta aceptada
Alexander
el 20 de Nov. de 2023
Editada: Alexander
el 21 de Nov. de 2023
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 comentarios
Alexander
el 21 de Nov. de 2023
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.
Walter Roberson
el 21 de Nov. de 2023
Files = ls('../..')
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', '../..')
Más respuestas (1)
Les Beckham
el 20 de Nov. de 2023
1 comentario
Cris LaPierre
el 20 de Nov. de 2023
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.
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!