Listing C: folder contents

Does anyone know why, in Windows 10 with R2021a, the command
dir("C:")
returns a listing of files in the current folder? I can't see this behaviour mentioned in the documentation.
I realise that
dir("C:\")
does what I'd expect, so I can solve the problem in my own code, but I'm wondering whether to report this as a bug, or whether I've missed something.
When I have another drive attached,
dir("D:")
and
dir("D:\")
behave identically to each other (listing the top-level folder contents for the drive), and I'd expect the results to be consistent across drives.
The dir command in the Windows command shell behaves in this way, but that's no reason for the MATLAB command to do the same thing, unless it's stated in the MATLAB documentation.

Answers (1)

dpb
dpb on 15 Jun 2021
"The dir command in the Windows command shell behaves in this way, but that's no reason for the MATLAB command to do the same thing,..."
I think it's a very good reason for MATLAB to do so; it's consistent with the OS (and more than likely all dir() is doing is passing the input string to the OS command and returning its output in the consistent struct, anyway).
It's nothing new nor different, earlier releases and versions of Windows also behave the same way.
The documentation is mum on the precise subject -- it doesn't make any claims one way or t'other for the particular case.

3 Comments

Thanks dbp - but I don't agree. I have no reason to know in general what OS commands do - to me, the MATLAB dir function is part of MATLAB, not part of Windows, and should as far as possible be OS-independent. (In the way, for example, that fullfile and fileparts seek to support system independence.) If the function does something as unexpected as returning a listing for a directory that isn't its argument, that needs to be flagged up in the documentation so that users don't waste time finding it out when their code doesn't work - or at least the documentation should refer to the underlying OS command.
And - sorry to be argumentative - I don't think what earlier versions did is relevant, because there will (I hope!) always be new users, and users like me who have been using MATLAB for a long time but who haven't previously hit this particular problem.
Well, the last point first is that changing behavior may break previous code so changing behavior isn't free--it may not be an issue for the new user, but those (like me also) who've used it for a long time could be bit.
As for the first, I would argue if one is using the product on the OS, consistency with the OS is to be desired; I often will pass the command to the OS in order to get back what can sometimes be done there that can't in the neutered MATLAB version -- other switches or wildcard patterns, etc., etc., ... if they returned different results, that's also a pain to handle.
Now, whether Bill and company did right by implementing it the way they did is a whole different story; I wouldn't argue there that alternate choices could have been made.
I just wouldn't expect either to change at this late date, though...although I could see there being some additional caveats in the ML documentation, maybe.
Sure - I do agree the behaviour shouldn't change now - I guess I'm mainly thinking the documentation could have been more helpful. I'll send in feedback by the official mechanism.

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 15 Jun 2021

Commented:

on 15 Jun 2021

Community Treasure Hunt

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

Start Hunting!