mkdir — Create folders in MATLAB and RunMat.
mkdir creates folders on disk using single-path or parent/child path forms. Status/message outputs and filesystem error behavior follow MATLAB semantics.
Syntax
status = mkdir(folderName)
status = mkdir(parentFolder, folderName)
[status, msg, msgID] = mkdir(folderName)
[status, msg, msgID] = mkdir(parentFolder, folderName)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
folderName | StringScalar | Yes | — | Folder path to create. |
parentFolder | StringScalar | Yes | — | Parent folder path. |
folderName | StringScalar | Yes | — | Child folder path under parentFolder. |
Returns
| Name | Type | Description |
|---|---|---|
status | NumericScalar | 1 on success, 0 when creation fails. |
msg | StringScalar | Diagnostic message for failures. |
msgID | StringScalar | Stable diagnostic identifier string. |
Returned values from mkdir depend on how many outputs the caller requests.
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:mkdir:NotEnoughInputs | No input arguments are provided. | mkdir: not enough input arguments |
RunMat:mkdir:TooManyInputs | More than two input arguments are provided. | mkdir: too many input arguments |
RunMat:mkdir:FolderArgType | Folder argument is not a character vector or string scalar. | mkdir: folder name must be a character vector or string scalar |
RunMat:mkdir:ParentArgType | Parent argument is not a character vector or string scalar. | mkdir: parent folder must be a character vector or string scalar |
RunMat:mkdir:OSError | Directory creation fails due to filesystem error. | mkdir: unable to create folder |
RunMat:mkdir:DirectoryExists | Target directory already exists. | Directory already exists. |
RunMat:mkdir:ParentDirectoryDoesNotExist | Parent folder does not exist. | mkdir: parent folder does not exist |
RunMat:mkdir:ParentIsNotDirectory | Parent or target path exists as a non-directory entry. | mkdir: target path is not a directory |
RunMat:mkdir:InvalidFolderName | Folder name is empty. | Folder name must not be empty. |
How mkdir works
status = mkdir(folder)createsfolder, returning1when the directory is created or already exists, and0on failure. Status outputs aredoublescalars for MATLAB compatibility.[status, message, messageID] = mkdir(parent, child)creates the directoryfullfile(parent, child). When the directory is created during this call, the message outputs are empty. If the directory already exists, MATLAB populates them with'Directory already exists.'and'RunMat:MKDIR:DirectoryExists'. Failures return the system error text and message identifier.- The optional
messageandmessageIDoutputs are character arrays (with size1×0when empty), matching MATLAB’s behaviour. Callers that prefer string scalars can wrap them withstring(message). - Passing a parent folder that does not exist leaves the filesystem unchanged and returns
status = 0. - If the target path already exists as a file,
mkdirfails gracefully withstatus = 0and a diagnostic message; it does not overwrite files. - Path arguments accept character vectors or string scalars. Other input types raise
mkdir: folder name must be a character vector or string scalar(or the equivalent message for the parent argument). - The builtin expands
~to the user’s home directory and honours relative paths with respect to the current working folder (pwd).
Does RunMat run mkdir on the GPU?
mkdir performs host-side filesystem operations. When callers supply GPU-resident scalars (for example, gpuArray("logs")), RunMat gathers the value back to the CPU before resolving the path. Acceleration providers do not publish hooks for this builtin, so there is no device-side implementation to enable.
GPU memory and residency
No. mkdir executes entirely on the host—GPU residency provides no benefit. However, if a script accidentally stores path strings on the GPU, RunMat automatically gathers them before accessing the filesystem so the call still succeeds.
Examples
Create A New Folder In The Current Directory
status = mkdir("results")Expected output:
status =
1Create A Nested Folder Using Parent And Child Arguments
mkdir("data");
status = mkdir("data", "archive/2024")Expected output:
status =
1Detect When A Folder Already Exists
mkdir("logs");
[status, message, messageID] = mkdir("logs")Expected output:
status =
1
message =
Directory already exists.
messageID =
RunMat:MKDIR:DirectoryExistsHandle Missing Parent Folder Gracefully
[status, message, messageID] = mkdir("missing-parent", "child")Expected output:
status =
0
message =
Parent folder "missing-parent" does not exist.
messageID =
RunMat:MKDIR:ParentDirectoryDoesNotExistCapture Detailed Status And Messages
[status, message, messageID] = mkdir("reports")Expected output:
status =
1
message =
messageID =Create A Folder In Your Home Directory With Tilde Expansion
status = mkdir("~", "RunMatProjects")Expected output:
status =
1Use gpuArray Inputs For Paths
status = mkdir(gpuArray("gpu-output"))Expected output:
status =
1Using mkdir with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how mkdir changes the result.
Run a small mkdir example, explain the result, then change one input and compare the output.
FAQ
What status codes does mkdir return?⌄
1 indicates success (the directory exists afterwards), and 0 indicates failure. Status values are doubles to match MATLAB.
Does mkdir overwrite existing files?⌄
No. If the target path already exists as a regular file, mkdir returns status = 0 and reports that the path is not a directory.
Can I create multiple levels of folders at once?⌄
Yes when you provide a single path, because RunMat mirrors MATLAB’s behaviour of creating intermediate directories. When using the two-argument form, the parent folder must already exist.
Does mkdir support string scalars and character vectors?⌄
Yes. String arrays must contain exactly one element; other types raise an error.
How are error messages returned?⌄
Failures return descriptive messages and MATLAB-style message IDs (for example, RunMat:MKDIR:OSError) in the second and third outputs. The builtin does not throw unless the inputs are invalid.
Are UNC paths and drive-letter paths supported on Windows?⌄
Yes. Provide the path exactly as you would in MATLAB; RunMat forwards it to the operating system.
Can I run mkdir on the GPU?⌄
No. The function operates on the host, but it automatically gathers GPU-resident inputs for convenience.
What happens if the folder already exists?⌄
mkdir reports success (status = 1) and leaves the directory untouched, just like MATLAB.
Does tilde (~) expand to the home directory?⌄
Yes. Both single-argument and two-argument forms expand ~ at the start of a path.
How do I handle errors programmatically?⌄
Capture the optional outputs and test status. When it is 0, inspect message and messageID for diagnostics.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how mkdir is executed, line by line, in Rust.
- View the source for mkdir in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.