Skip to content

Instantly share code, notes, and snippets.

@tiborsimon
Last active October 13, 2015 22:26
Show Gist options
  • Save tiborsimon/c3d1140d87614961d283 to your computer and use it in GitHub Desktop.
Save tiborsimon/c3d1140d87614961d283 to your computer and use it in GitHub Desktop.
Simple Input Parser code samples
function ret = my_function(varargin)
params.a = 42;
params.b = 'answer';
params.c = 55.3;
params = simple_input_parser(params, varargin);
% further functionalities that uses the params struct
end
ssin('A f L fs phi', 2, 440, 0.8, 48e3, 45)
ssin('AfLfsphi', 2, 440, 0.8, 48e3, 45)
function ret = my_function(varargin)
params.a = 0;
params.b = 0;
params.c = 0;
[params, flags] = simple_input_parser(params, varargin);
if flags.b
disp('b parameter was passed');
end
% further functionalities
end
ssin('f', 440, 'A', 2, 'phi', 45, 'fs', 48e3, 'L', 0.8)
% use this function to generate a sine wave
ssin(440, 2, 45, 48e3, 0.8)
Running SimpleInputParserTests
..........
..........
..........
...
Done SimpleInputParserTests
__________
result
_________________________________
[1x33 matlab.unittest.TestResult]
function [validflag, errormsg] = my_validator_for_the_parameter_c(value)
validflag = value > 1;
errormsg = 'Parameter c has to be greater than 1..';
end
function ret = my_function(varargin)
params.a = 0;
params.b = 0;
params.c = 0;
function [validflag, errormsg] = my_validator_for_the_parameter_c(value)
validflag = value > 1;
errormsg = 'Parameter c has to be greater than 1..';
end
validators.c = @validate_c;
params = simple_input_parser(params, varargin, validators);
% further functionalities
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment