diff --git a/+types/+util/checkDims.m b/+types/+util/checkDims.m index 9e483059..1c21fd44 100644 --- a/+types/+util/checkDims.m +++ b/+types/+util/checkDims.m @@ -5,11 +5,21 @@ function checkDims(valueSize, validSizes, enforceScalarSize) % of valid sizes, validates that the value size matches at least one of them. % % types.util.CHECKDIMS(valsize, validSizes, enforceScalarSize) optionally -% enforces stricter validation for vectors. By default, MATLAB vectors -% (column: [n,1], row: [1,n]) pass validation against a 1D size (Inf), -% because vector data is typically treated as 1D when written to file. -% However, DataPipe may write vector data as 2D. To prevent 2D sizes from -% being accepted as 1D, use the enforceScalarSize flag. +% enforces stricter validation for scalar/1D shapes. By default, MATLAB +% vectors with shape [n,1] (column) or [1,n] (row) will pass validation +% against a 1D size specification of [Inf], because vector data is typically +% treated as 1D when written to file. However, in some cases (e.g., DataPipe +% maxSize), the 2D shape [n,1] or [1,n] determines the actual shape in the +% exported file. Set enforceScalarSize to true to reject 2D vector shapes +% when a 1D shape like [Inf] is specified for validSize. +% +% Example: +% % Without strict enforcement, [5,1] matches [Inf]: +% types.util.checkDims([5,1], {Inf}, false); % OK +% +% % With strict enforcement, [5,1] does NOT match [Inf]: +% types.util.checkDims([5,1], {Inf}, true); % Error +% types.util.checkDims(5, {Inf}, true); % OK if nargin < 3 % Set default value for enforceScalarSize if not provided enforceScalarSize = false; diff --git a/+types/+util/validateShape.m b/+types/+util/validateShape.m index c9205fb5..20250281 100644 --- a/+types/+util/validateShape.m +++ b/+types/+util/validateShape.m @@ -16,11 +16,13 @@ function validateShape(propertyName, validShapes, value) end elseif isa(value, 'types.untyped.DataPipe') valueShape = value.internal.maxSize; - % For data pipes, vectors can be exported to HDF5 files as 2D arrays - % (columnar (n,1) or row (1,n)). The types.util.checkDims function allows - % this, even if the valid shape specifies that the data should be 1D. - % Use 'enforceScalarShape' to ensure that 2D-like vectors do not - % pass validation when the valid shape specifies 1D data. + % For DataPipe objects, vectors can be exported to HDF5 files as 2D arrays + % with shape [n,1] (column) or [1,n] (row). By default, types.util.checkDims + % allows these 2D shapes to pass validation even when the valid shape + % specifies 1D data (e.g., [Inf]). However, for DataPipe, the maxSize + % property determines the actual shape in the exported file, so we need + % stricter validation. Set 'enforceScalarShape' to true to ensure that + % shapes like [n,1] or [1,n] are not accepted when [Inf] is specified. enforceScalarShape = true; elseif istable(value) valueShape = [height(value) 1]; @@ -49,7 +51,17 @@ function validateShape(propertyName, validShapes, value) % Check actual size of DataPipe and warn if it is not valid if isa(value, 'types.untyped.DataPipe') try + if enforceScalarShape + % Reset enforceScalarShape to false when validating the current + % data size. Strict validation is only needed for maxSize, which + % determines the final shape of the dataset in the exported file. + % The current data size is allowed to be more flexible (e.g., + % [n,1] can match [Inf]). + enforceScalarShape = false; + end + valueShape = size(value); + types.util.checkDims(valueShape, validShapes, enforceScalarShape); catch ME warning(...