MATLAB: How to check if I have read or write access to a directory

MATLAB

When I do 
>> fileattrib('\\path\to\my\folder')
"UserRead" and "UserWrite" is "1" even though Windows Explorer shows that I do not have read or write access in the folder properties. Furthermore, when I try to read or write in the directory, it fails. Why does "fileattrib" indicate that I have read and write access even though I do not?

Best Answer

The "fileattrib" function is written as a wrapper for the DOS "attrib" command on Windows. "attrib" cannot do all the operations that you expect and checking read or write access for directories may not return the expected result. Therefore, when you do "fileattrib" on a directory, "UserRead" and "UserWrite" does not reliably report the read and write access to the directory in question. 
One workaround to check read access to a folder is:
exist( myfolder, 'dir' ) && ~isempty( dir( myfolder ) )
To check if the user has write permission:
fileName = '\\path\to\my\folder\tmpFile.txt';
[fid,errmsg] = fopen(fileName, 'w');
if ~isempty(errmsg)&&strcmp(errmsg,'Permission denied')
fprintf('\nError: You do not have write permission to the folder (%s).\n',fileName);
else
fclose(fid);
delete(fileName);
end
Disclaimer : The above script deletes the file. Please take a backup of the file before running the script