problems using 'dir' and including search words

12 ビュー (過去 30 日間)
Kim
Kim 2012 年 11 月 16 日
コメント済み: Naum Derzhi 2016 年 9 月 7 日
Having problems using the function 'dir' in the following setting:
The goal is to make a structure array, each element containing the information about the files fullfilling the name-requirements specified in the command (see *).
dirData = dir([path,filename,'_Id = a*.log'])
()'path' is a well defined directory, 'filename' contains the first part of the name of each file(s) I need, '_Id=a.log' is the remaining part of the file(s) I need.
When attempting this in the command window, I simply obtain a structure array with zero elements, although I know that the directory implied by path contains 30 of these files.
Any help is much appreciated. Regards
  2 件のコメント
Yowh
Yowh 2012 年 11 月 16 日
Can you give me 2 - 3 example names of your files ?
Naum Derzhi
Naum Derzhi 2016 年 9 月 7 日
You need filesep between path and filename, otherwise you are not searching inside path directory. As a matter of taste, rather than writing
dir([path,filesep,filename,'_Id = a*.log'] )
I would use fullfile, like this:
dir(fullfile(path,[filename,'_Id = a*.log']))
These two expressions are equivalent.

サインインしてコメントする。

回答 (3 件)

Matt J
Matt J 2012 年 11 月 16 日
編集済み: Matt J 2012 年 11 月 16 日
Just a guess,
dirData = dir([path,filesep, filename,'_Id = a*.log'])
Otherwise, print [path, filename,'_Id = a*.log'] to the command window and see what you're really feeding to DIR.

Jan
Jan 2012 年 11 月 16 日
編集済み: Jan 2012 年 11 月 16 日
Do not shadow the very important function path() by a variable.
Matt J's suggestion to insert a file separator is a good idea, Sometimes folders are stored with a trailing separator (e.g. 'C:\'), sometimes without it. A flexible solution:
dirData = dir(fullfile(folder, [filename,'_Id = a*.log']));
Further idea: Do the filename contain spaces around the '='? In the text of the question you write: '_Id=a*.log' without spaces.
Does "struct array with zero elements" mean, that the array is empty, or that it contains zeros as elements?
Anyhow, if folder is really an existing path (please check this again), the dir command finds all matching file names. I promise that Matlab does not hide existing files magically, so there must be a bug elsewhere in the code.

Kim
Kim 2012 年 11 月 16 日
Thanks for all your comments and answers. I managed to find the problem by verifying my 'path' variable - it was missing a '\' at the end, leading to a directory that didn't exist.. Cheers

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by