Function with 'varargin' giving error when providing name-value pair only: MATLAB

Viewed 2783

I'm trying to build my own version of MATLAB's dir function. My current code (below) almost works but I am having trouble parsing a certain combination of inputs.

What I would like it to do is this:

  • Don't list hidden folders.
  • Have a Boolean parameter (default is false) to return only directory names and not files.
  • Have an optional field for the path of the folder (default is current directory).

To be clearer, I would like to create the function dir2 which can handle these combinations:

  1. dir2 This should list every not-hidden file or folder in the current directory
  2. dir2('path_to_directory') This should list every not-hidden file or folder in the specified directory
  3. dir2('OnlyDirectories', true) This should list only the not-hidden folders in the current directory
  4. dir2('path_to_directory', 'OnlyDirectories', true) This should list only the not-hidden folders in the specified directory

My current version is this:

function list = dir2(varargin)
    p = inputParser;

    addOptional(p, 'name', '.', @ischar);
    addParameter(p, 'OnlyDirectories', false, @islogical);
    parse(p, varargin{:});

    list = dir(p.Results.name);
    if p.Results.OnlyDirectories
        dirFlags = [list.isdir];
        list = list(dirFlags); % Keeping only directories
    end
    % Deleting hidden folders from the list
    list = list(arrayfun(@(x) ~strcmp(x.name(1),'.'), list)); 
end

This works fine for cases 1, 2 and 4 but it doesn't work for case 3. In this case it gives me the error:

Expected a string scalar or character vector for the parameter name, instead the input type was 'logical'.

I think I might be missing something trivial about MATLAB's input parsing but I can't figure out what.

2 Answers
Related