Filter löschen
Filter löschen

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

114 Ansichten (letzte 30 Tage)
Jan Böttner
Jan Böttner am 29 Aug. 2023
Kommentiert: Jan Böttner am 29 Aug. 2023
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Aug. 2023
Bearbeitet: Stephen23 am 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 Kommentar
Jan Böttner
Jan Böttner am 29 Aug. 2023
thanks a lot, works perfectly! Wasn't aware of the syntax difference :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 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.
Jan Böttner
Jan Böttner am 29 Aug. 2023
thanks for your help and the advice!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by