Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

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

Date: 2022-12-03 11:53:50

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 12 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. 

Expert Answer:

s: 

You're right that the parser appears to give some odd results, a related question might be this one.

One work-around, which works for your function in its current form, would be to add a check for if 2 inputs are given. If there are 2 inputs, assume it's your OnlyDirectories flag and use the default name value. The code would look like this and passes all 4 of your example use cases. 

 

function list = dir2(varargin) p = inputParser; addOptional(p, 'name', '.', @ischar); addParameter(p, 'OnlyDirectories', false, @islogical); if numel(varargin) == 2 varargin = [{'.'}, varargin]; end parse(p, varargin{:}); list = dir(p.Results.name); if p.Results.OnlyDirectories dirFlags = [list.isdir]; list = list(dirFlags); end list = list(arrayfun(@(x) ~strcmp(x.name(1),'.'), list)); end 

This is a bit hacky though, and has scope to give confusing error messages. It would be better to just have both inputs as name-value pairs 

 

function list = dir2(varargin) p = inputParser; addParameter(p, 'name', '.', @ischar); addParameter(p, 'OnlyDirectories', false, @islogical); % ... other code end

 

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com