ls

Lists the names of the files and folders in a folder.

Category: Programming > Files and Folders

Syntax

ls

Displays the contents of the current folder.

ls name

Displays the items matching a folder name or a pattern.

list = ls(name)

Returns the names as a padded character matrix, one row per item.

Description

ls is a quick way to look at a folder. As a command, ls data prints the names in columns. With an output argument it returns a char matrix that includes . and .. for a folder listing; cellstr(ls(...)) turns it into a cell array of names, and dir gives the richer struct form with sizes and dates.

Input arguments

name
Folder or pattern such as 'data/*.csv'.

Output arguments

list
Character matrix, rows padded with blanks. Convert with cellstr.

Example

Printing and collecting names

mkdir data
writelines('1', 'data/x.txt');
writelines('2', 'data/y.csv');
ls data
names = cellstr(ls('data/*.csv'))
Output

.      ..     x.txt  y.csv  

names = 1×1 cell

    {'y.csv'}

See also

dir what cd cellstr