Can't create a folder outside of the current directory

4 visualizaciones (últimos 30 días)
I am using a package on Matlab, and in order for that to function properly, I need to keep the path folder fixed.
Now in my code, I import a data from a path. Then I generate plots, which I would like to save in a different folder. More specifically, in a sub-folder inside the folder, from which I imported the data in the first place.
I did it using the mkdir function like this:
% path to files
pname = 'C:\Package\Matlab Codes\';
% which files to be imported
fname = [pname '\MyData.txt'];
%create a subfolder
mkdir pname MyFolder
However, running the above code results in creating a folder names 'pname' in my current directory. MyFolder is created inside the pname. Somehow it doesn't understand that the pname is the same adress as above. If I write
'C:\Package\Matlab Codes\'
in place of
pname
, it works properly.
Does anyone how to fix it? seems to be a syntaxing issue.

Respuesta aceptada

Steven Lord
Steven Lord el 12 de En. de 2022
Use fullfile instead of manually concatenating the path and file name then use the function form of mkdir rather than the command form.
% path to files
pname = 'C:\Package\Matlab Codes\';
% which files to be imported
fname = fullfile(pname, 'MyFolder', 'MyData.mat');
%create a subfolder using the function form of mkdir
mkdir(fullfile(pname, 'MyFolder'))
% Create a file
x = 1:10;
save(fname, 'x')
The following two lines of code are equivalent and explain why the directory named pname was created.
mkdir pname
mkdir('pname')

Más respuestas (0)

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by