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

114 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2023-8-29
编辑:Stephen23 2023-8-29
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 个)

Image Analyst
Image Analyst 2023-8-29
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 个评论
Image Analyst
Image Analyst 2023-8-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by