问题
I tried to use:
dir('dirname\*')
and it did not work. It started to work after I started to use:
dir('dirname\m*')
Does anybody know why?
回答1:
Matlab does understand wildcards *
, but in the way you unluckiliy tried to adhere to Windows cmd path conventions, you entered the string \*
, which is a literal asterisk (due to the escaping backslash).
A workaround, or the preferred way to specify paths on all platforms, is using a forward slash /
as a directory seperator.
dir('dirname/*')
This also explains why adding the m after the backslash "fixed" the issue; the asterisk was no longer a literal asterisk, but allowed to be interpreted as a wildcard character.
EDIT: Documentation explicitely says the wildcard character is allowed and works as expected (see my explanation above).
回答2:
Try to provide the full path, like dir('c:\dirname*.m'), and make sure the folder 'dirname' exists.
回答3:
What is your OS ? Here on Windows, the first line works well. However, the "*" is maybe considered by Matlab as a literal "*". What happens with dir('dirname/*')
?
来源:https://stackoverflow.com/questions/5337846/why-matlab-does-not-understand-in-the-names-of-files