Creating folders outside of the current folder

Hi!
I would like to use MATLAB to create folders in a specified folder. However, I've only been able to get MATLAB to create folder within the current folder (cd) using the 'mkdir' command. Is it possible for it to create folders outside of the current one if it is told where to create it?
Thank you!

 Accepted Answer

Of course, just define the parentFolder, or use a relative/absolute path:
I also recommend using function syntax:
Putting it all together:
mkdir(ParentFolder,NewSubFolder)
mkdir(fullfile(ParentFolder,NewSubFolder))

5 Comments

Chinwe Orie's "Answer" moved here:
Hi Stephen,
Thank you for answering!
I tried doing that with the following code but I get an error in the mkdir line.
The error went away when I removed the paranthesis from the mkdir line, but it didn't create the folder in the selected folder. It went right back to creating the new folder in the current folder.
source_dir = uigetdir(); %allows user to pick the folder to create the file in
ParentFolder = source_dir; %defines the user selection as the parent folder
NewSubFolder = fullfile(source_dir,'rawdata'); %defines location of the new folder
mkdir(ParentFolder,NewSubFolder);
Your code defines the source_dir both as the ParentFolder and also as part of the NewSubFolder. I don't see that in my answer or in the MATLAB documentation.
Following exactly what I wrote in my answer, use either of these:
mkdir(source_dir,'rawdata')
mkdir(fullfile(source_dir,'rawdata'))
Worked great. Thank you!
How could I go about creating a folder outside of the current folder without specifiying the parent folder name?
"How could I go about creating a folder outside of the current folder without specifiying the parent folder name?"
Creating a folder requires telling the OS where to create that folder: this may be implicit (e.g. in the current directory) or explicit using a relative/absolute filepath. Perhaps a relative path does what you want.
@Cory Dinkle: please give an example of the path information you have, and the folder that you expect that information to create.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Asked:

on 2 May 2019

Edited:

on 21 Dec 2021

Community Treasure Hunt

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

Start Hunting!