필터 지우기
필터 지우기

wildcard in filename for readtable does not work

조회 수: 32 (최근 30일)
Lieke van Boxtel
Lieke van Boxtel 2023년 11월 20일
편집: Stephen23 2023년 11월 21일
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
Stephen23
Stephen23 2023년 11월 20일
편집: Stephen23 2023년 11월 20일
"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:

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

채택된 답변

Alexander
Alexander 2023년 11월 20일
편집: Alexander 2023년 11월 21일
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
Alexander
Alexander 2023년 11월 21일
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
Walter Roberson 2023년 11월 21일
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 '

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

추가 답변 (1개)

Les Beckham
Les Beckham 2023년 11월 20일
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
Cris LaPierre
Cris LaPierre 2023년 11월 20일
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.

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by