Can't create a folder outside of the current directory
2 ビュー (過去 30 日間)
古いコメントを表示
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.
採用された回答
Steven Lord
2022 年 1 月 12 日
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')
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!