Skip to content
Snippets Groups Projects
Verified Commit 9622fc96 authored by Thomas Lambert's avatar Thomas Lambert :helicopter:
Browse files

refactor(utils): improve cyclomatic complexity

parent 63645207
No related branches found
No related tags found
No related merge requests found
function [filenames, filepaths, idxOpts] = parsefileinputs(optList, filetype, varargin)
function [filenames, filespath, idxOpts] = parsefileinputs(optList, filetype, varargin)
% PARSEFILEINPUTS Parses the input and checks their validity.
% Returns the filenames and filepaths for the input files, as well as the index where the
% remaining options start.
......@@ -37,7 +37,7 @@ function [filenames, filepaths, idxOpts] = parsefileinputs(optList, filetype, va
if hasNoInput
% Prompt user to select files
ext = ['*', filetype];
[filenames, filepaths] = uigetfile(ext, 'Select all dat-files to aggregate', ...
[filenames, filespath] = uigetfile(ext, 'Select all dat-files to aggregate', ...
'MultiSelect', 'on');
idxOpts = 1;
else
......@@ -50,34 +50,14 @@ function [filenames, filepaths, idxOpts] = parsefileinputs(optList, filetype, va
inputFiles = DEFAULT_INPUTFILES;
end
% Validate inputDir
validateattributes(inputDir, {'char', 'string'}, {'nonempty', 'vector'}, ...
mfilename(), 'inputDir');
if ~exist(inputDir, 'dir')
error('MATLAB:parsefileinputs:dirNotFound', ...
'The directory specified as inputDir (%s) can not be found!\n', inputDir);
end
% Validate inputFiles
validateattributes(inputFiles, {'char', 'cell'}, {'nonempty', 'vector'}, ...
mfilename(), 'inputFiles', 4);
if iscell(inputFiles) && ~iscellstr(inputFiles)
error('MATLAB:parsefileinputs:wrongInputFiles', ...
['If inpuFiles is given as a cell array, '...
'it must contain only character vectors!\n']);
end
% Validate inputs
validateinputdir(inputDir);
validateinputfiles(inputFiles);
% ---------------------------------------
% Look for files
% Get absolute path
if contains(inputDir, pwd)
filepaths = inputDir;
else
filepaths = fullfile(pwd, inputDir);
end
filespath = absolutepath(inputDir);
% Convert inputFiles to string for simpler handling
inputFiles = string(inputFiles);
......@@ -87,10 +67,10 @@ function [filenames, filepaths, idxOpts] = parsefileinputs(optList, filetype, va
for i = 1:length(inputFiles)
inputFiles(i) = appendextension(inputFiles(i), filetype); % Add extension
dummy = dir(fullfile(filepaths, inputFiles(i)));
dummy = dir(fullfile(filespath, inputFiles(i)));
if isempty(dummy)
warning('MATLAB:parsefileinputs:FileNotFound', ...
'Could not find file %s.', fullfile(filepaths, inputFiles(i)));
'Could not find file %s.', fullfile(filespath, inputFiles(i)));
end
AllFiles = [AllFiles; dummy];
end
......@@ -119,3 +99,43 @@ function bool = isoption(optList, var)
bool = any(strcmpi(var, optList));
end
end
% --------------------------------------------------------------------------------------------------
function abspath = absolutepath(directory)
% ABSOLUTEPATH Returns the absolute path to a directory
if contains(directory, pwd)
abspath = directory;
else
abspath = fullfile(pwd, directory);
end
end
% --------------------------------------------------------------------------------------------------
function validateinputdir(inputDir)
% VALIDATEDIR Validates inputDir input.
validateattributes(inputDir, {'char', 'string'}, {'nonempty', 'vector'}, ...
mfilename(), 'inputDir');
if ~exist(inputDir, 'dir')
error('MATLAB:parsefileinputs:dirNotFound', ...
'The directory specified as inputDir (%s) can not be found!\n', inputDir);
end
end
% --------------------------------------------------------------------------------------------------
function validateinputfiles(inputFiles)
% VALIDATEDIR Validates inputDir input.
validateattributes(inputFiles, {'char', 'cell'}, {'nonempty', 'vector'}, ...
mfilename(), 'inputFiles', 4);
if iscell(inputFiles) && ~iscellstr(inputFiles)
error('MATLAB:parsefileinputs:wrongInputFiles', ...
['If inpuFiles is given as a cell array, '...
'it must contain only character vectors!\n']);
end
end
......@@ -51,16 +51,31 @@ function [digits, nPoints, idxOpts] = parsenacainputs(optList, varargin)
idxOpts = 2;
digits = varargin{1};
if length(varargin) >= 2
if ~isoption(optList, varargin{2})
idxOpts = 3;
nPoints = varargin{2};
end
if length(varargin) >= 2 && ~isoption(optList, varargin{2})
idxOpts = 3;
nPoints = varargin{2};
end
end
% Validate NACA digits input
% Validate inputs
validatedigits(digits);
validatenpoints(nPoints);
end
% --------------------------------------------------------------------------------------------------
function bool = isoption(optList, var)
% ISOPTION Returns true if var is contained in option list
bool = any(strcmpi(var, optList));
end
% --------------------------------------------------------------------------------------------------
function validatedigits(digits)
% VALIDATEDIGITS Validates digit input.
% Correct type
validateattributes(digits, {'char', 'string'}, {'scalartext'}, mfilename(), 'digits', 1);
if isstring(digits)
digits = char(digits);
......@@ -69,13 +84,13 @@ function [digits, nPoints, idxOpts] = parsenacainputs(optList, varargin)
% Only accept NACA 4- or 5-digit
if length(digits) < 4 || length(digits) > 5
error('MATLAB:parsenacainputs:wrongNumberOfDigits', ...
['nacacamber and nacaairfoil can only be used for NACA 4 or 5 digits airfoils.']);
'nacacamber and nacaairfoil can only be used for NACA 4 or 5 digits airfoils.');
end
% Ensures the character vector only contains digits
if ~all(isstrprop(digits, 'digit'))
error('MATLAB:parsenacainputs:invalidDigits', ...
['The input (''%s'') does not correspond to a proper NACA airfoil.'], digits);
'The input (''%s'') does not correspond to a proper NACA airfoil.', digits);
end
% Ensure the third number of a NACA-5 is either 0 or 1
......@@ -87,9 +102,15 @@ function [digits, nPoints, idxOpts] = parsenacainputs(optList, varargin)
'(standard camber) or a 1 (reflex camber). Found %d'], q);
end
end
end
% --------------------------------------------------------------------------------------------------
function validatenpoints(nPoints)
% VALIDATENPOINTS Validates nPoints input.
% Validate nPoints input
% Correct type
validateattributes(nPoints, {'numeric'}, {'scalar', '>=', 2}, mfilename(), 'nPoints', 2);
% Enough points to have a good result
if nPoints <= 10
warning('MATLAB:parsenacainputs:lowNPoints', ...
['The number of points is very low (%d), ' ...
......@@ -97,10 +118,3 @@ function [digits, nPoints, idxOpts] = parsenacainputs(optList, varargin)
end
end
% --------------------------------------------------------------------------------------------------
function bool = isoption(optList, var)
% ISOPTION Returns true if var is contained in option list
bool = any(strcmpi(var, optList));
end
......@@ -31,12 +31,6 @@ function [alpha, cl, cd, cm, idxOpts] = parsepolarinputs(optList, varargin)
% First ensures the option list is properly set
validateattributes(optList, {'char', 'string', 'cell'}, {}, mfilename(), 'optList', 1);
% Initialize outputs
alpha = [];
cl = [];
cd = [];
cm = [];
% Parse inputs
hasNoInput = isempty(varargin) || isoption(optList, varargin{1});
if hasNoInput
......@@ -54,19 +48,7 @@ function [alpha, cl, cd, cm, idxOpts] = parsepolarinputs(optList, varargin)
validateattributes(Polar, {'struct'}, {'nonempty'}, mfilename(), 'Polar', 1);
[alpha, cl, cd, cm] = extractpolar(varargin{1});
else
% Parse user-provided arrays
if length(varargin) >= 1
[alpha, idxOpts, foundOpts] = assignvar(optList, alpha, varargin, 1);
if length(varargin) >= 2 && ~foundOpts
[cl, idxOpts, foundOpts] = assignvar(optList, cl, varargin, 2);
if length(varargin) >= 3 && ~foundOpts
[cd, idxOpts, foundOpts] = assignvar(optList, cd, varargin, 3);
if length(varargin) >= 4 && ~foundOpts
[cm, idxOpts, ~] = assignvar(optList, cm, varargin, 4);
end
end
end
end
[alpha, cl, cd, cm, idxOpts] = parsearrays(optList, varargin);
end
end
......@@ -80,9 +62,9 @@ function [alpha, cl, cd, cm, idxOpts] = parsepolarinputs(optList, varargin)
validateattributes(alpha, {'numeric'}, {'real', '2d', 'nonempty', 'increasing'}, ...
mfilename(), 'alpha', 1);
checkarray(cl, size(alpha, 1), 2);
checkarray(cd, size(alpha, 1), 3);
checkarray(cm, size(alpha, 1), 4);
validatearray(cl, size(alpha, 1), 2);
validatearray(cd, size(alpha, 1), 3);
validatearray(cm, size(alpha, 1), 4);
% Standardize alpha size so we have one column per Polar as well
if size(alpha, 2) == 1 && ~isempty(cl)
......@@ -109,9 +91,11 @@ function [alpha, cl, cd, cm] = extractpolar(Polar)
end
% --------------------------------------------------------------------------------------------------
function [var, idxOpts, foundOpts] = assignvar(optList, var, data, idx)
function [var, idxOpts, foundOpts] = assignvar(optList, data, idx)
% ASSIGNVAR Assign varargin value to a variable, ouptut corresponding idxOpts.
var = [];
foundOpts = false;
if ~isoption(optList, data{idx})
idxOpts = idx + 1;
......@@ -124,8 +108,32 @@ function [var, idxOpts, foundOpts] = assignvar(optList, var, data, idx)
end
% --------------------------------------------------------------------------------------------------
function checkarray(array, nrows, idx)
% CHECKARRAY Checks type and diemnsions of array.
function [alpha, cl, cd, cm, idxOpts] = parsearrays(optList, data)
% PARSEARRAYS Parse user-provided arrays.
alpha = [];
cl = [];
cd = [];
cm = [];
if length(data) >= 1
[alpha, idxOpts, foundOpts] = assignvar(optList, data, 1);
if length(data) >= 2 && ~foundOpts
[cl, idxOpts, foundOpts] = assignvar(optList, data, 2);
if length(data) >= 3 && ~foundOpts
[cd, idxOpts, foundOpts] = assignvar(optList, data, 3);
if length(data) >= 4 && ~foundOpts
[cm, idxOpts, ~] = assignvar(optList, data, 4);
end
end
end
end
end
% --------------------------------------------------------------------------------------------------
function validatearray(array, nrows, idx)
% VALIDATEARRAY Checks type and diemnsions of array.
if ~isempty(array)
validateattributes(array, {'numeric'}, {'real', 'nrows', nrows, 'nonempty'}, ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment