Best practice for validation of two related inputs in Matlab

Ben

I have a question about the best practices for input validation in Matlab functions with multiple inputs. It's a bit philosophical. I've looked around in the forum and I have not seen a comprehensive discussion of this. I care about the case where the validation conditions involves two or more of the input variables. Here is an example.

Suppose that I write a function with two inputs, a and b. I know that the inputs must satisfy the conditions

a > 0 and b > 0.

If I want to validate these inputs, I would normally write a function like this (just for illustration purposes, what the function does is not important):

% My function
function [result] = myLogSum(a,b)

% Create an input parser
p = inputParser;
% Add required inputs with validation
isPositive = @(x) all(x>0);
addRequired(p, 'a', isPositive);
addRequired(p, 'b', isPositive);
% Parse the inputs
parse(p,a,b);

% Calculate the log sum
result = log(a) + log(b);

end

But now suppose that a and b are arrays, and that I also need to check if they are the same size:

all(size(a) == size(b)) == true.

Is there a way to deal with such a situation with the input parser? If not, what is the best way to deal with this?

I can think of four solutions, but I can't figure out which is the best.

1) Should I lump a and b in a single input cell array variable sumInput of the form {a,b} and write a custom validation function for the cell array? That's fine, but I don't know if it's always good practice to lump inputs together like that. Here it seems very natural to be able to write myLogSum(a,b) instead of myLogSum(sumInput).

2) Or in such instances, should I write this part of the input validation in the function, i.e., modify the above code like this:

% My function
function [result] = myLogSum(a,b)

% Create an input parser
p = inputParser;
% Add required inputs with validation
isPositive = @(x) all(x>0);
addRequired(p, 'a', isPositive);
addRequired(p, 'b', isPositive);
% Parse the inputs
parse(p,a,b);

% Check that the input arrays have the same size
if ~all(size(a)==size(b))
   message = 'The arrays a and b must have the same size.';
   error(message);
end

% Calculate the log sum
result = log(a) + log(b);

end

That's fine too, but it makes the validation a bit inhomogeneous and inaesthetic because now I the inputs a and b are validated twice in different ways and for different reasons.

3) Should I just give up on the input parser and just write my own validation functions, as is suggested here:

Best practice when validating input in MATLAB

But I quite like the input parser because it's a neat way to manage options.

4) I could just let Matlab handle the error by itself when the program reaches the last line result = log(a) + log(b) and the array sizes don't match. But somehow I feel like this is asking for trouble in the long run.

If you have experience in Matlab, just let me know what you think is the most robust and comprehensive validation strategy when the two inputs are related.

Ben

nekomatic

There's nothing to stop you from calling parse once, then adding new inputs and calling parse again - at which point you can use the previously parsed value(s) in your validation function. The validateattributes function is useful here for building validation functions with multiple conditions. For example:

% My function
function [result] = myLogSum(a,b)

% Create an input parser
p = inputParser;
% Add required inputs with validation
addRequired(p, 'a', @(x) validateattributes(x, {'numeric'}, {'>', 0}));
% Check the first input was valid
p.parse(a);
addRequired(p, 'b', @(x) validateattributes(x, {'numeric'}, {'>', 0, 'size', size(a)}));
% Parse the inputs
p.parse(a,b);

% Calculate the log sum
result = log(a) + log(b);

end

validateattributes also generates reasonably explanatory error messages for you:

>> myLogSum(30, 40)

ans =

    7.0901

>> myLogSum([30 20], 40)
Error using myLogSum (line 12)
The value of 'b' is invalid. Expected input to be of size 1x2 when it is
actually size 1x1.

>> myLogSum([30 20], [40 1])

ans =

    7.0901    2.9957

>> myLogSum([30 20], [40 -1])
Error using myLogSum (line 12)
The value of 'b' is invalid. Expected input to be an array with all of the
values > 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django validation best practice

From Dev

best practice for data manipulation + validation

From Java

Best Practice: Input Validation (Android)

From Dev

Best practice checking token validation

From Dev

angularjs custom validator for two related inputs

From Dev

MongoDB best practice to count related documents

From Dev

What is the best practice querying related table with EF

From Dev

Best practice for selecting two columns

From Dev

Time validation between two time inputs

From Dev

Best practice for train, validation and test set

From Dev

JWT password validation best practice advice

From Dev

What is the best practice for when to do input validation?

From Dev

Kotlin Spring Request Blank Validation Best Practice

From Dev

What is the Numpy best practice for a function that works with scalars or vectors as inputs?

From Dev

Objective C - Best practice for Unit Testing with multiple test inputs

From Dev

Matlab - Singleton expansion for more than two inputs

From Dev

Validation depending on two inputs using availity-reactstrap-validation

From Dev

AWS DLQ for two consumers, what is best practice?

From Dev

Best practice for calling two different endpoints for a widget?

From Dev

Best Practice to chain two functions,, AngularJs/JavaScript

From Dev

Cypress Best Practice - Store and compare two values

From Dev

Best practice to work on two github go projects

From Dev

Angular: Elegant way to toggle visibility of two related inputs

From Dev

seeking Best practice advice related to managing iBeacon localnotifications

From Dev

REST URL best practice for id-related objects

From Dev

Django related_name naming best practice in case of multiple relations

From Dev

Best practice to have documentation and other project related files in Git?

From Dev

What's the best practice for deprecating a set of related methods?

From Dev

Best practice for deleting related entities in normalized state in Redux

Related Related

  1. 1

    Django validation best practice

  2. 2

    best practice for data manipulation + validation

  3. 3

    Best Practice: Input Validation (Android)

  4. 4

    Best practice checking token validation

  5. 5

    angularjs custom validator for two related inputs

  6. 6

    MongoDB best practice to count related documents

  7. 7

    What is the best practice querying related table with EF

  8. 8

    Best practice for selecting two columns

  9. 9

    Time validation between two time inputs

  10. 10

    Best practice for train, validation and test set

  11. 11

    JWT password validation best practice advice

  12. 12

    What is the best practice for when to do input validation?

  13. 13

    Kotlin Spring Request Blank Validation Best Practice

  14. 14

    What is the Numpy best practice for a function that works with scalars or vectors as inputs?

  15. 15

    Objective C - Best practice for Unit Testing with multiple test inputs

  16. 16

    Matlab - Singleton expansion for more than two inputs

  17. 17

    Validation depending on two inputs using availity-reactstrap-validation

  18. 18

    AWS DLQ for two consumers, what is best practice?

  19. 19

    Best practice for calling two different endpoints for a widget?

  20. 20

    Best Practice to chain two functions,, AngularJs/JavaScript

  21. 21

    Cypress Best Practice - Store and compare two values

  22. 22

    Best practice to work on two github go projects

  23. 23

    Angular: Elegant way to toggle visibility of two related inputs

  24. 24

    seeking Best practice advice related to managing iBeacon localnotifications

  25. 25

    REST URL best practice for id-related objects

  26. 26

    Django related_name naming best practice in case of multiple relations

  27. 27

    Best practice to have documentation and other project related files in Git?

  28. 28

    What's the best practice for deprecating a set of related methods?

  29. 29

    Best practice for deleting related entities in normalized state in Redux

HotTag

Archive