Matlab list_files


Massachusetts Institute of Technology


April 24, 2018

My preferred programing language is R. But, for many purposes, I find myself in need of a Graphical User Interface (GUI). Thus, I experienced a forceful transition to Matlab. Let’s be honest, Matlab can do powerful things, and it’s a great language to attempt to dominate (note to self: learn Python!1). Still, I find myself over and over thinking in R mode. Something along the lines of:

Gimme all the files in folder with xyz…

Can be easily accomplished in R with list.files()


This command can handle many many options, with pattern being among my favorite. More importantly, this command returns a useful character vector. No extra dots, no list of lists, no array. Just, useful. On the other hand, Matlab has dir and ls both of which are not satisfactory.



Yes… Matlab’s version can also handle some form of regular expression matching. But, mind the dots and the structure! Matlab’s dir has a ton of things on it.

>> q = dir

q = 

  20×1 struct array with fields:


Matlab ls function is also full of deadly traps.

qq = ls

qq =

  20×28 char array

    '.                           '
    '..                          '
    'many_things_here.ext        '

The solution

I found myself fighting for a character vector or array (nx1) that I could feed into a function/loop/whatever.

Thus, after many many many times fighting against classes, with functions that expect char instead of string, or cell, or whatever, I decided to create something that resembles (at least partially) the functionality I was looking for. It’s not perfect. Please enlighten me with a better approach. For now, I will be using list_files.m. Wanna use it? Be my guest, see below:

Show Matlab Code
% The idea of this function is to have something that works to list files
% Matlab has too many weird things with dir/patterns/etc...
% It could be slow if calling in a BIG dir and then subsetting
% Otherwise it should work pretty fast

function filenames = list_files(varargin)

% Open input parser
p = inputParser();

% Add possible values
addOptional(p, 'Interactive', true, @islogical)
addOptional(p, 'Dirname', '0', @ischar)
addOptional(p, 'Pattern', {'.'}, @iscell)
addOptional(p, 'FullPath', false, @islogical)

% parse
parse(p, varargin{:});

% retrieve things from parser
Interactive = p.Results.Interactive;
Dirname = p.Results.Dirname;
Pattern = p.Results.Pattern;
FullPath = p.Results.FullPath;

%% Dirname goes first
% If we didn't provide a Dirname, both defaults will hold
% If we provided a Dirname, we will read from there

if (Interactive && string(Dirname) == '0')

    dirname = uigetdir();

    dirname = Dirname;

if ~isdir(dirname)
    error('Dirname not valid, check dirname provided is character and exists.')

% actually call dir
% Remove the dots matlab puts to things

    % Get filenames
    % Output as an mx1 cell
    filenames = {}';

%% Subset by pattern
    default_pattern = string(Pattern) == '.';

    if (~default_pattern) % non default case
    % join cell patterns separated by the 'or' regular expression
    query_expression = strjoin(Pattern, '|');
    %  Subset the patterns
    filenames = filenames(~cellfun(@isempty,regexp(filenames, query_expression)));
    % By default we return just the name
    % If you want the full path, call it!
    % it currently works only for 1 folder
    % Recursive = TRUE will be super nice!
    if FullPath
    filenames = fullfile(unique({d.folder}), filenames);


  1. In 2023, having learned Python, I celebrate that my Matlab days are long over. That being said, I ended up writing an rlist_files package for Python due to fighting against os and Path libraries. You can find it here.↩︎



BibTeX citation:
  author = {Andina, Matias},
  title = {Matlab List\_files},
  date = {2018-04-24},
  url = {},
  langid = {en}
For attribution, please cite this work as:
Andina, Matias. 2018. “Matlab List_files.” April 24, 2018.

Enjoy my creations?

I'm so glad you're here. As you know, I create a blend of fiction, non-fiction, open-source software, and generative art - all of which I provide for free.

Creating quality content takes a lot of time and effort, and your support would mean the world to me. It would empower me to continue sharing my work and keep everything accessible for everyone.

How can you support my work?

There easy ways to contribute. You can buy me coffee, become a patron on Patreon, or make a donation via PayPal. Every bit helps to keep the creative juices flowing.

Become a Patron!

Share the Love

Not in a position to contribute financially? No problem! Sharing my work with others also goes a long way. You can use the following links to share this post on your social media.

Affiliate Links

Please note that some of the links above might be affiliate links. At no additional cost to you, I will earn a commission if you decide to make a purchase.

© CC-By Matias Andina, 2023 | This page is built with ❤️ and Quarto.