How to solve error "access denied" when using mkdir?

114 views (last 30 days)
Hi,
i want to create a new folder.
It works when I do it like this:
mkdir '\\dbfz-user.leipzig.dbfz.de\user$\jboettner\Documents\HiWi\Matlab\File import\angepasste_Ausgabedatein' Plots
But when i read in the file path (as I use it elsewhere in the script) it does not work:
P = '\\dbfz-user.leipzig.dbfz.de\user$\jboettner\Documents\HiWi\Matlab\File import\angepasste_Ausgabedatein'
mkdir P Plots
I get an error message saying that the access is denied. Is there any way to solve this? As in my view, I doesn't make sense that one would work and for the other one the access is denied.

Accepted Answer

Stephen23
Stephen23 on 29 Aug 2023
Edited: Stephen23 on 29 Aug 2023
Explanation:
The basic problem is that you are calling MKDIR using command syntax (not function syntax), but are expecting to provide an input argument. That will not work.
Your command syntax:
mkdir P Plots
is exactly equivalent to this function syntax:
mkdir('P','Plots')
but clearly your OS does let you create things in the non-existent folder P.
Solution:
If you want to call any function with an input argument (not literal text) then use function syntax:
mkdir(P,'Plots')
With a few exceptions (e.g. debugging) you should forget that command syntax exists.
  1 Comment
Jan Böttner
Jan Böttner on 29 Aug 2023
thanks a lot, works perfectly! Wasn't aware of the syntax difference :)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Aug 2023
This is what I'd do
folder = '\\dbfz-user.leipzig.dbfz.de\user$\jboettner\Documents\HiWi\Matlab\File import\angepasste_Ausgabedatein'
if ~isfolder(folder)
mkdir(folder);
end
What is "Plots" supposed to do and why is it on the line where you're trying to create a folder? Is it a subfolder of the first folder? If so, just add it on:
folder = '\\dbfz-user.leipzig.dbfz.de\user$\jboettner\Documents\HiWi\Matlab\File import\angepasste_Ausgabedatein/Plots'
if ~isfolder(folder)
mkdir(folder);
end
  3 Comments
Image Analyst
Image Analyst on 29 Aug 2023
Then you can't hard code the folder like that if it's for different users and P changes somehow. So let's say that you got P somehow for the current user. Then you could just do
mkdir(P, 'Plots');
Or even better;
plotFolder = [P, 'Plots'];
if ~isfolder(plotFolder)
mkdir(plotFolder);
end
Why is this better? Now you can use plotFolder later on in your code whenever you need to. Otherwise if you just refer to it when you're calling mkdir, you won't have it later when you need it.
Also, another tip is to use fullfile and to never use cd to change the current folder, use fullfile() instead.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!