フィルターのクリア

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

114 ビュー (過去 30 日間)
Jan Böttner
Jan Böttner 2023 年 8 月 29 日
コメント済み: Jan Böttner 2023 年 8 月 29 日
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 件のコメント
Jan Böttner
Jan Böttner 2023 年 8 月 29 日
thanks a lot, works perfectly! Wasn't aware of the syntax difference :)

サインインしてコメントする。

その他の回答 (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.
Jan Böttner
Jan Böttner 2023 年 8 月 29 日
thanks for your help and the advice!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by