New to coding - Error /.DS_Store (Name is nonexistent or not a directory) on MacBook

Hi all,
I am new to coding with zero prior experience. I have copied an existing code from a PC (expecting it to be plug and play) and it throws up an error saying "x/.DS_Store (Name is nonexistent or not a directory)". I reckon this should be a quick fix for someone who works with Matlab on macs. Please help me to get rid of this problem!
Cheers,
AS

9 Comments

@AS - when do you see this error message? Is this when you try to run the code or something else? If something requires this file, then I suppose you could create one. To do this, you can use the terminal application and change directory to your folder x. From there just open the finder as
xxx@MacBook-Pro x % open .
(i.e. write open .) and that should open the Finder and create the .DS_Store folder (or at least it did for me).
yes I get this error when I try to run the code. I tried your suggestion and unfortunately it did not work. :/
@AS - can you post the full error message? Can you confirm if the file is created in your x folder by (again from the terminal) running the command
xxx@MacBook-Pro x % ls -ag
after you have opened the Finder. When I tried this, the .DS_Store file was created.
@Geoff Hayes it did not create the .DS_Store file in my x folder.
This is the error I am getting when I run the code:
Error using cd
Cannot CD to /x/.DS_Store (Name is nonexistent or not a directory).
Error in code (line 18)
cd(foldername);
@AS - if you put a breakpoint at this line of code
cd(foldername);
what is foldername? Is it a valid path? How is it set? I don't understand why the code would try to change directory to something that is a filename (.DS_Store)...
@Geoff Hayes yes, foldername is a valid path. The code works like a charm on other systems.
@AS - can you show some of the code that is calling cd? I'm just curious why it it doing that. Else, you can add code like @Image Analyst shows below to skip this file (which is specific to Mac and so that may be why you aren't seeing this on the Windows platform).
"cd(foldername);"
Someone wrote slow fragile code that:
  • uses CD() when using absolute/relative filenames is more robust and more efficient,
  • iterates over everything that DIR() returns, without using a suitable match string or filtering the results (e.g. based on the ISDIR property), i.e. fragile/buggy code (as your experience using it demonstrates).
Basically you should NOT learn from this code.
Hi all, the code now works after asking it to ignore the "DS_store" file. A colleage helped me integrate that into the existing code. Many thanks!

Sign in to comment.

 Accepted Answer

I think that's just some sort of system bookkeeping file on Macs. It's not a file you'd ever want to process in any way so just skip it in your loop:
folder = pwd; % wherever you want
fileList = dir(fullfile(folder, '*.*'))]
% If you want all the names put one cell array:
allFileNames = fullfile({fileList.folder}, {fileList.name})'
% Now files are listed in the order you called dir().
% If you want the list sorted alphabetically:
allFileNames = sort(allFileNames)
for k = 1 : numel(allFileNames)
thisFileName = allFileNames{k};
if contains(thisFileName, 'DS_Store')
% Skip this kind of file.
continue;
end
% If it gets here, the file is good so process it.
end

5 Comments

@Image Analyst thanks. My brain is fried now, I will try this first thing on Monday.
My guess is that the current code does a dir() and assumes that it can cd() to every name it finds there. Rather than special-casing that one name, the code should check whether the name is a directory before trying to cd()
In the longer term, it is typically better to use fullfile() to construct fully qualified names instead of using cd() at all.
The code now works after asking it to ignore the "DS_store" file. A colleage helped me integrate that into the existing code. Many thanks!
OK since my solution of skipping that particular file worked, could you please click the "Accept this answer" link? Thanks in advance. 🙂

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Asked:

AS
on 24 Jun 2022

Commented:

AS
on 5 Jul 2022

Community Treasure Hunt

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

Start Hunting!