Creating and deleting directories files and folders in Matlab

Here I will explain how to create and delete files or directories in Matlab. This can be used to copy or move files as well. In fact, Matlab can issue any system command. I will show how to do it on Linux platform since its what I use everyday. Basically, we can use a default Matlab function that is called system. It executes a command just like if you were typing it in the terminal.

To create a directory:

system('mkdir my_new_directory_name');

Now lets try to save the output of the system command in Matlab. Lets try to list whats inside the current directory using ls command.

[status,result]=system('ls');

Now lets look at the output:

status =
0
result =
my_new_directory_name

The current directory now contains my_new_directory_name as expected!

Now lets move into this directory

system('cd my_new_directory_name');

Easy!

You can programatically issue commands:

i=1
folder_name=['folder',num2str(i)]
command=['mkdir ',folder_name]
system(command)

We have just created the folder “folder1” !

One thought on “Creating and deleting directories files and folders in Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *