mkdir — Create directories and return logical status with optional diagnostics.
mkdir creates a directory hierarchy through the active filesystem service.
Syntax
[status, msg, msgID] = mkdir(folderName)
[status, msg, msgID] = mkdir(parentFolder, folderName)Inputs
folderName- Type
StringScalar- Required
- Yes
- Description
- Folder path to create, including missing intermediate folders.
parentFolder- Type
StringScalar- Required
- Yes
- Description
- Parent path to create when necessary.
folderName- Type
StringScalar- Required
- Yes
- Description
- Relative child path beneath parentFolder.
Returns
status- Type
LogicalArray- Description
- Logical true when the folder exists after the operation.
msg- Type
StringScalar- Description
- Character-row diagnostic, empty after a newly created folder.
msgID- Type
StringScalar- Description
- Character-row diagnostic identifier.
Returned values from mkdir depend on how many outputs the caller requests.
Errors
RunMat:mkdir:InvalidArgumentCount- When
- The call has zero or more than two inputs.
- Message
mkdir: expected one folder or a parent and relative child
RunMat:mkdir:InvalidFolder- When
- A folder input is not a character row or string scalar.
- Message
mkdir: folder names must be character vectors or string scalars
RunMat:mkdir:InvalidFolderName- When
- A required folder name is empty.
- Message
mkdir: folder name must not be empty
Create directories
mkdir(folderName) creates the named directory and any missing intermediate directories. mkdir(parentFolder, folderName) creates a relative child beneath the parent and creates the parent when necessary.
With outputs, status is a logical scalar. A newly created directory returns true with empty character rows for msg and msgID. An existing directory also returns true and supplies an informational message and identifier.
Errors and execution
When outputs are requested, an operational filesystem failure returns false with diagnostic text. Without outputs, the same failure raises an error; an existing directory emits a warning governed by the session's warning settings.
Folder names accept character rows and scalar strings. Relative paths use the current folder, a leading ~ expands through the active environment, and native Windows paths may use drive-letter or UNC syntax. The filesystem service receives the path without rewriting platform-specific components.
Directory creation runs through the host or browser filesystem service rather than an acceleration provider. Folder names must be host text; numeric or provider-resident values are rejected without a device transfer.
Examples
Create a directory
folder = tempname();
status = mkdir(folder);
existsAfter = exist(folder, 'dir');
rmdir(folder);Expected output:
status is logical true and existsAfter is nonzeroCreate a missing parent and nested child
parent = tempname();
status = mkdir(parent, fullfile('archive', '2026'));
created = exist(fullfile(parent, 'archive', '2026'), 'dir');
rmdir(parent, 's');Expected output:
status is logical true and the nested directory existsInspect an existing-directory result
folder = tempname();
mkdir(folder);
[status, msg, msgID] = mkdir(folder);
rmdir(folder);Expected output:
status is true and msg and msgID describe the existing directoryCreate intermediate directories from one path
root = tempname();
folder = fullfile(root, 'data', 'archive');
status = mkdir(folder);
created = exist(folder, 'dir');
rmdir(root, 's');Expected output:
status is logical true and every path component is createdCreate a directory from a character row
folder = char(tempname());
status = mkdir(folder);
created = exist(folder, 'dir');
rmdir(folder);Expected output:
character-row paths use the same filesystem operationCapture a target-file failure
target = tempname();
fid = fopen(target, 'w');
fclose(fid);
[status, msg, msgID] = mkdir(target);
stillFile = exist(target, 'file');
delete(target);Expected output:
status is logical false, diagnostics are populated, and the file remainsInspect successful diagnostic outputs
folder = tempname();
[status, msg, msgID] = mkdir(folder);
messageSize = size(msg);
idSize = size(msgID);
rmdir(folder);Expected output:
successful creation returns empty character rowsUsing 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
Does mkdir create intermediate directories?⌄
Yes. Both the single-path form and a missing parent in the two-input form create the required directory chain.
What happens when the target exists as a file?⌄
The file is never overwritten. Output forms return logical false and diagnostics; a no-output call raises an error.
Can mkdir run on a GPU?⌄
No. Directory creation is a filesystem operation, and its text path arguments do not require device transfers.
Which path value types does mkdir accept?⌄
A character row, scalar string, or one-element string array is accepted. Other values reject before filesystem access.
How do I handle creation failures programmatically?⌄
Request status, message, and message ID. Status is logical false after an operational failure; the other outputs describe it. A no-output call raises an error.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · filesep · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · memmapfile.subsasgn · memmapfile.subsref · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Tabular
arrayDatastore · csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · getvaropts · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · setvaropts · setvartype · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Data
commit · data.copy · data.create · data.delete · data.exists · data.export · data.import · data.inspect · data.list · data.move · data.open · DataArray.chunk_shape · DataArray.codec · DataArray.dtype · DataArray.fill · DataArray.name · DataArray.rank · DataArray.read · DataArray.resize · DataArray.shape · DataArray.write · Dataset.array · Dataset.arrays · Dataset.attrs · Dataset.begin · Dataset.get_attr · Dataset.has_array · Dataset.id · Dataset.path · Dataset.refresh · Dataset.set_attr · Dataset.set_attrs · Dataset.snapshot · Dataset.version · DataTransaction.abort · DataTransaction.commit · DataTransaction.create_array · DataTransaction.delete_array · DataTransaction.fill · DataTransaction.id · DataTransaction.resize · DataTransaction.set_attr · DataTransaction.set_attrs · DataTransaction.status · DataTransaction.write
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.