diff --git a/+quantity/Discrete.m b/+quantity/Discrete.m index 027ed5598ec90b1750e31139493d92d1aeb3e72a..a7d8568a561dd9f7042515ca6673ec1dc3febee6 100644 --- a/+quantity/Discrete.m +++ b/+quantity/Discrete.m @@ -59,7 +59,7 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi % quantity.Discrete obj = valueOriginal; else - % empty object. this is neede for instance, to create + % empty object. this is needed for instance, to create % quantity.Discrete([]), which is useful for creating default % values. obj = quantity.Discrete.empty(size(valueOriginal)); @@ -1594,8 +1594,18 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi % ISEMPTY checks if the quantity object is empty % empty = isempty(obj) - % check if there is already an object - empty = any(size(obj) == 0) || any(cellfun(@iscell, [obj.grid])); + % check if there is any dimension which is zero + empty = any(size(obj) == 0); + + % if the constructor is called without arguments, a + % quantity.Discrete is initialized without an initialization of + % the grid. Thus, the grid is not a cell: + empty = empty || ~iscell(obj(1).grid); + + % in order to check this for arrays: + % scalar and array empty check has to be separated because the + % [obj.grid] does only work in the array case. + empty = empty || any(cellfun(@iscell, [obj.grid])); end % isempty() @@ -2288,7 +2298,14 @@ classdef (InferiorClasses = {?quantity.Symbolic}) Discrete < handle & matlab.mi % end function s = getPropertyGroups(obj) - s = getPropertyGroups@matlab.mixin.CustomDisplay(obj(1)); + % Function to display the correct values + + if isempty(obj) + s = getPropertyGroups@matlab.mixin.CustomDisplay(obj); + return; + else + s = getPropertyGroups@matlab.mixin.CustomDisplay(obj(1)); + end if numel(obj) ~= 1 s.PropertyList.valueDiscrete = ... diff --git a/+unittests/+quantity/testDiscrete.m b/+unittests/+quantity/testDiscrete.m index a53959b712d94ba2cabd071fe311bc38f86617cc..28d04c565c9b809ac092bac224d2a0cf3ea05353 100644 --- a/+unittests/+quantity/testDiscrete.m +++ b/+unittests/+quantity/testDiscrete.m @@ -1246,9 +1246,16 @@ end function testIsEmpty(tc) - +% create an empty quantity and check if it is empty: +e = quantity.Discrete.empty([5, 0]); +tc.verifyTrue(isempty(e)); -%verifyTrue(tc, isempty(quantity.Discrete())) +% create an empty object by calling the constructor without arguements. +tc.verifyTrue(isempty(quantity.Discrete())); + +% create a constant quantity.Discrete. This should not be empty: +c = quantity.Discrete(1, 'grid', {}, 'gridName', {}); +tc.verifyTrue(~isempty(c)); end