mkdir

Creates a folder, including any missing parent folders.

Category: Programming > Files and Folders

Syntax

mkdir(folder)

Creates folder.

mkdir(parent, name)

Creates name inside parent.

[status, message, messageid] = mkdir(___)

Returns the outcome instead of only acting.

Description

Creating a folder that already exists is not an error: the call reports success and, if outputs are requested, a message saying that the directory already exists. This makes mkdir safe to call at the top of a script that saves results.

The command form mkdir results works like the function call.

Input arguments

folder
Folder to create, relative or absolute. Intermediate folders are created too.
parent, name
Parent folder and new folder name, as separate arguments.

Output arguments

status
true if the folder exists afterwards.
message
Text such as 'Directory already exists.', empty on a plain success.
messageid
Message identifier, for instance 'MATLAB:MKDIR:DirectoryExists'.

Example

Nested folders and a repeat call

mkdir out/2024/jan
isfolder('out/2024/jan')
[status, msg, id] = mkdir('out/2024/jan')
Output
ans = logical
   1
status = logical
   1
msg = 'Directory already exists.'
id = 'MATLAB:MKDIR:DirectoryExists'

See also

rmdir cd isfolder copyfile movefile