Skip to content

Commit 6ffc2d6

Browse files
committed
h5save: check dataset vs shape if specified
1 parent 1ad73dd commit 6ffc2d6

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

+stdlib/+hdf5nc/private/defaultSize.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function s = defaultSize(A)
2-
3-
if isvector(A)
2+
if isscalar(A)
3+
s = 0;
4+
elseif isvector(A)
45
s = length(A);
56
else
67
s = size(A);

+stdlib/+hdf5nc/private/h5_exist_file.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ function h5_exist_file(filename, varname, A, sizeA)
55
diskshape = h5size(filename, varname);
66
if length(diskshape) >= 2
77
% start is always a row vector, regardless of shape of array
8-
start = ones(1,ndims(A));
8+
start = ones(1, ndims(A));
99
elseif ~isempty(diskshape)
1010
start = 1;
1111
end
1212

1313
if isempty(sizeA)
1414
sizeA = defaultSize(A);
15+
elseif all(sizeA > 0)
16+
assert(numel(A) == prod(sizeA), 'hdf5nc:h5save:shape_error', "dataset # of elements %d != prod(sizeA) %d", numel(A), prod(sizeA))
17+
elseif ~isscalar(sizeA)
18+
error('hdf5nc:h5save:shape_error', "only scalar size may be 0")
1519
end
1620

1721
if isscalar(A)

+stdlib/+hdf5nc/private/h5_new_file.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
function h5_new_file(filename, varname, A, sizeA)
22

33
if isempty(sizeA)
4-
if isscalar(A)
5-
sizeA = 0;
6-
elseif isvector(A)
7-
sizeA = length(A);
8-
else
9-
sizeA = size(A);
10-
end
4+
sizeA = defaultSize(A);
5+
elseif all(sizeA > 0)
6+
assert(numel(A) == prod(sizeA), 'hdf5nc:h5save:shape_error', "dataset # of elements %d != prod(sizeA) %d", numel(A), prod(sizeA))
7+
elseif ~isscalar(sizeA)
8+
error('hdf5nc:h5save:shape_error', "only scalar size may be 0")
119
end
1210

1311
if isscalar(sizeA)

0 commit comments

Comments
 (0)