Main Content

mustBeNonsparse

Validate that value is nonsparse

Description

example

mustBeNonsparse(value) throws an error if value is sparse. This function does not return a value.

mustBeNonsparse calls the following function to determine if the input is not 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)
Error using mustBeNonsparse (line 13)
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 being 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
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 the following:

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
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™.

Introduced in R2017a