Borrar filtros
Borrar filtros

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

156 visualizaciones (últimos 30 días)
Jan Böttner
Jan Böttner el 29 de Ag. de 2023
Comentada: Jan Böttner el 29 de Ag. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 29 de Ag. de 2023
Editada: Stephen23 el 29 de Ag. de 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 comentario
Jan Böttner
Jan Böttner el 29 de Ag. de 2023
thanks a lot, works perfectly! Wasn't aware of the syntax difference :)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 29 de Ag. de 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 comentarios
Image Analyst
Image Analyst el 29 de Ag. de 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 el 29 de Ag. de 2023
thanks for your help and the advice!

Iniciar sesión para comentar.

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by