Main Content

mustBeNonsparse

Validate that value is nonsparse

Description

example

mustBeNonsparse(value) throws an error if value is sparse. If value is empty, the function does not throw an error. This function does not return a value.

mustBeNonsparse calls the issparse function to determine if the input is sparse.

Class support: All numeric classes, logical, and MATLAB® classes that overload issparse.

Examples

collapse all

Use mustBeNonsparse to validate that the input is nonsparse.

Use the sparse function to create a sparse matrix.

A = [ 0   0   0   5
      0   2   0   0
      1   3   0   0
      0   0   4   0];
S = sparse(A);

Validate that S is nonsparse.

mustBeNonsparse(S)
Values must not be sparse.

This class restricts the value of Prop1 to nonsparse values.

classdef MyClass
   properties
      Prop1 {mustBeNonsparse}
   end
end

Create an object and assign a value to its property.

obj = MyClass;
A = [ 0   0   0   5
      0   2   0   0
      1   3   0   0
      0   0   4   0];
obj.Prop1 = sparse(A);
Error setting 'Prop1' property of 'MyClass' class:
Values must not be sparse.

When you assign a value to the property, MATLAB calls mustBeNonsparse with the value being assigned to the property. mustBeNonsparse issues an error because the value assigned to Prop1 is sparse.

This function restricts the input argument to be any nonsparse value.

function mbNonsparse(S)
    arguments
        S {mustBeNonsparse}
    end
    disp(S)
end

Calling this function with a sparse array results in an error thrown by mustBeNonsparse.

A = [ 0   0   0   5
      0   2   0   0
      1   3   0   0
      0   0   4   0];
S = sparse(A);
mbNonsparse(S)
Error using mbNonsparse
 mbNonsparse(S)
             ↑
Invalid input argument at position 1. Value must not be sparse.

Input Arguments

collapse all

Value to validate, specified as a scalar or array of any one of these types:

Other data types cause an error.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Number Support: Yes

Tips

  • mustBeNonsparse is designed to be used for property and function argument validation.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2017a