Possible bug in Matlab's "fileattrib" function. Workaround?

664 views Asked by At

I have found some strange behaviour in Matlab's fileattrib function on Windows. With certain file names it wrongly identifies the file as a hidden, system folder.

To test it, download this file (the file is empty; it's only the file name that matters):

https://docs.google.com/file/d/0B9BeckFuQk1bNHY3T0NKaFpxbUU/edit?usp=sharing

Put the file on an empty folder (I'm using "c:\temp") and try this:

fileattrib('c:\temp\*')

If your Matlab is like mine, it will give you this wrong result:

ans = 
            Name: 'c:\temp\?aaa.txt'
         archive: 1
          system: 1
          hidden: 1
       directory: 1
              [...]

Now rename the file name removing the first character and try again. It will correctly say

ans = 
            Name: 'c:\temp\aaa.txt'
         archive: 1
          system: 0
          hidden: 0
       directory: 0
            [...]

I have seen this behaviour in Matlab R2010b and R2007a, on Windows Vista and 7.

The problem clearly has to do with certain "offending" characters (or character sets/encodings?), but I've no idea. Can someone figure out why this happens? And how to work around it?

EDIT:

This seems to have been corrected in R2015a (maybe earlier): it correctly returns

        Name: 'C:\Users\Luis\Desktop\tmp\�aaa.txt'
     archive: 1
      system: 0
      hidden: 0
   directory: 0
        [...]
1

There are 1 answers

1
Dennis Jaheruddin On

One way to deal with this is not to depend (solely) on the fileattrib command.

In order to determine whether something is a file or directory, you can check how it registers when using the dir command on the containing folder.

Its a bit of a hassle, but when using dir called on the folder (won't work when called on the file directly) you seem to get the correct output.


A quick and dirty alternative would of course be to put your entire handling in a try / catch construction and if one fails simply try the other.