Skip to content

Commit 3e56792

Browse files
committed
use R2019b+ arguments validation
1 parent e4e3c40 commit 3e56792

File tree

13 files changed

+95
-100
lines changed

13 files changed

+95
-100
lines changed

+hdf5nc/auto_chunk_size.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
% automatically determine HDF5 / NetCDF4 chunk size.
33
% based on https://github.com/h5py/h5py/blob/master/h5py/_hl/filters.py
44
% refer to https://support.hdfgroup.org/HDF5/Tutor/layout.html
5-
narginchk(1,1)
6-
validateattributes(dims, {'numeric'}, {'vector', 'integer', 'positive'})
5+
arguments
6+
dims (1,:) {mustBeInteger,mustBePositive}
7+
end
78

89
CHUNK_BASE = 16000; % Multiplier by which chunks are adjusted
910
CHUNK_MIN = 8000; % lower limit: 8 kbyte

+hdf5nc/coerce_ds.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
function A = coerce_ds(A, dtype)
22
% used by h5save and ncsave
3-
narginchk(2,2)
4-
validateattributes(A, {'numeric'}, {'nonempty'},1)
5-
validateattributes(dtype, {'char'}, {'vector'},2)
3+
arguments
4+
A {mustBeNumeric,mustBeNonempty}
5+
dtype (1,1) string
6+
end
67

78
switch dtype
89
case {'float64', 'double'}
@@ -28,4 +29,4 @@
2829
otherwise, error('create_ds:type_error', 'unknown data type %s', dtype)
2930
end
3031

31-
end % function
32+
end % function

+hdf5nc/expanduser.m

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function expanded = expanduser(p)
22
% expanded = expanduser(path)
33
%
4-
% expands tilde ~ into user home directory for Matlab and GNU Octave.
4+
% expands tilde ~ into user home directory
55
%
66
% Useful for Matlab functions like h5read() and some Computer Vision toolbox functions
77
% that can't handle ~ and Matlab does not consider it a bug per conversations with
@@ -18,38 +18,24 @@
1818
% timeit(f)
1919
%
2020
% See also absolute_path
21-
22-
narginchk(1,1)
23-
24-
if isempty(p)
25-
expanded = '';
26-
return
21+
arguments
22+
p (1,1) string
2723
end
2824

29-
validateattributes(p, {'char'}, {'vector'}, 1)
30-
3125
expanded = p;
3226

33-
if strcmp(expanded(1), '~')
34-
35-
home = [];
36-
if isunix
37-
home = getenv('HOME');
38-
elseif ispc
39-
home = getenv('USERPROFILE');
40-
end
27+
if ~startsWith(expanded, "~")
28+
return
29+
end
4130

42-
if isempty(home)
43-
if usejava('jvm')
44-
% this is 100x slower than getenv() on Matlab R2020a
45-
home = char(java.lang.System.getProperty("user.home"));
46-
else
47-
% return unmodified
48-
return
49-
end
50-
end
31+
if ispc
32+
home = getenv('USERPROFILE');
33+
else
34+
home = getenv('HOME');
35+
end
5136

52-
expanded = fullfile(home, expanded(2:end));
37+
if ~isempty(home)
38+
expanded = fullfile(home, extractAfter(expanded, 1));
5339
end
5440

5541
end %function

+hdf5nc/h5exists.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
function exists = h5exists(filename, varname)
2-
% check if variable exists in HDF5 file
1+
function exists = h5exists(filename, varnames)
2+
% check if variable(s) exists in HDF5 file
33
%
44
% filename: HDF5 filename
55
% varname: name of variable inside HDF5 file
66
%
77
% exists: boolean
8+
arguments
9+
filename (1,1) string
10+
varnames (1,:) string
11+
end
812

9-
narginchk(2,2)
10-
validateattributes(filename, {'char'}, {'vector'}, 1)
11-
validateattributes(varname, {'char'}, {'vector'}, 2)
13+
if startsWith(varnames, "/")
14+
varnames = extractAfter(varnames, 1);
15+
end
1216

13-
exists = any(strcmp(hdf5nc.h5variables(filename), varname(2:end)));
17+
vars = hdf5nc.h5variables(filename);
18+
19+
exists = false(size(varnames));
20+
for i = 1:length(varnames)
21+
exists(i) = any(vars == varnames(i));
22+
end
1423

1524
end % function

+hdf5nc/h5save.m

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ function h5save(filename, varname, A, sizeA, dtype)
22
% H5SAVE
33
% create or append to HDF5 file
44
% parent folder (file directory) must already exist
5-
6-
narginchk(3, 5)
5+
arguments
6+
filename (1,1) string
7+
varname (1,1) string
8+
A {mustBeNumeric,mustBeNonempty}
9+
sizeA (1,:) {mustBeInteger,mustBeNonnegative} = []
10+
dtype (1,1) string = ""
11+
end
712

813
filename = hdf5nc.expanduser(filename);
914

10-
if nargin < 4 || isempty(sizeA)
15+
if isempty(sizeA)
1116
if isvector(A)
1217
sizeA = length(A);
1318
else
1419
sizeA = size(A);
1520
end
1621
end
17-
18-
if nargin >= 5 && ~isempty(dtype)
22+
% coerce if needed
23+
if dtype ~= ""
1924
A = hdf5nc.coerce_ds(A, dtype);
2025
end
2126
if ischar(A)
@@ -27,15 +32,13 @@ function h5save(filename, varname, A, sizeA, dtype)
2732
exist_file(filename, varname, A, sizeA)
2833
else
2934
new_file(filename, varname, A, sizeA)
30-
end % if
35+
end
3136

3237
end % function
3338

3439

3540
function exist_file(filename, varname, A, sizeA)
3641

37-
narginchk(4,4)
38-
3942
diskshape = hdf5nc.h5size(filename, varname);
4043
if length(diskshape) >= 2
4144
% start is always a row vector, regardless of shape of array
@@ -58,7 +61,6 @@ function exist_file(filename, varname, A, sizeA)
5861

5962

6063
function new_file(filename, varname, A, sizeA)
61-
narginchk(4,4)
6264

6365
folder = fileparts(filename);
6466
assert(isfolder(folder), '%s is not a folder, cannot create %s', folder, filename)

+hdf5nc/h5size.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
% variable: name of variable inside HDF5 file
66
%
77
% fsize: vector of variable size per dimension
8-
9-
narginchk(2,2)
10-
validateattributes(filename, {'char'}, {'vector'}, 1)
11-
validateattributes(variable, {'char'}, {'vector'}, 2)
8+
arguments
9+
filename (1,1) string
10+
variable (1,1) string
11+
end
1212

1313
finf = h5info(hdf5nc.expanduser(filename), variable);
1414
fsize = finf.Dataspace.Size;

+hdf5nc/h5variables.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
function [names, groups] = h5variables(filename, group)
22
% get dataset names and groups in an HDF5 file
3+
arguments
4+
filename (1,1) string
5+
group (1,1) string = ""
6+
end
37

4-
narginchk(1,2)
5-
6-
names = [];
7-
groups = [];
8+
names = string([]);
9+
groups = string([]);
810

9-
% use temporary variable to be R2017b OK
1011
finf = h5info(hdf5nc.expanduser(filename));
1112
ds = finf.Datasets;
1213
if isempty(ds)
1314
return
1415
end
15-
if nargin > 1
16+
17+
if group ~= ""
1618
gs = finf.Groups;
1719
i = contains({gs(:).Name}, group);
1820
if ~any(i)
@@ -21,11 +23,11 @@
2123
ds = gs(i).Datasets;
2224
end
2325

24-
names = {ds(:).Name};
26+
names = string({ds(:).Name});
2527

2628
if nargout > 1
2729
gs = finf.Groups;
28-
groups = {gs.Name};
30+
groups = string({gs.Name});
2931
end
3032

3133
end % function

+hdf5nc/ncexists.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
function exists = ncexists(filename, varname)
2-
% check if variable exists in NetCDF4 file
1+
function exists = ncexists(filename, varnames)
2+
% check if variable(s) exists in NetCDF4 file
33
%
44
% filename: NetCDF4 filename
55
% varname: name of variable inside file
66
%
77
% exists: boolean
8+
arguments
9+
filename (1,1) string
10+
varnames (1,:) string
11+
end
812

9-
narginchk(2,2)
10-
validateattributes(filename, {'char'}, {'vector'}, 1)
11-
validateattributes(varname, {'char'}, {'vector'}, 2)
13+
vars = hdf5nc.ncvariables(filename);
1214

13-
exists = any(strcmp(hdf5nc.ncvariables(filename), varname));
15+
exists = false(size(varnames));
16+
for i = 1:length(varnames)
17+
exists(i) = any(vars == varnames(i));
18+
end
1419

1520
end % function

+hdf5nc/ncsave.m

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
function ncsave(filename, varname, A, ncdims, dtype)
2-
3-
narginchk(3, 5)
2+
% NCSAVE
3+
% create or append to NetCDF4 file
4+
% parent folder (file directory) must already exist
5+
arguments
6+
filename (1,1) string
7+
varname (1,1) string
8+
A {mustBeNumeric,mustBeNonempty}
9+
ncdims cell = {}
10+
dtype (1,1) string = ""
11+
end
412

513
filename = hdf5nc.expanduser(filename);
614

7-
if nargin >= 4 && ~isempty(ncdims)
15+
if ~isempty(ncdims)
816
for i = 2:2:length(ncdims)
917
sizeA(i/2) = ncdims{i};
1018
end
@@ -28,7 +36,7 @@ function ncsave(filename, varname, A, ncdims, dtype)
2836
end % if
2937
end
3038
% coerce if needed
31-
if nargin >= 5 && ~isempty(dtype)
39+
if dtype ~= ""
3240
A = hdf5nc.coerce_ds(A, dtype);
3341
end
3442

@@ -44,8 +52,6 @@ function ncsave(filename, varname, A, ncdims, dtype)
4452

4553
function exist_file(filename, varname, A, sizeA)
4654

47-
narginchk(4,4)
48-
4955
diskshape = hdf5nc.ncsize(filename, varname);
5056

5157
if all(diskshape == sizeA)
@@ -61,8 +67,6 @@ function exist_file(filename, varname, A, sizeA)
6167

6268
function new_file(filename, varname, A, sizeA, ncdims)
6369

64-
narginchk(5,5)
65-
6670
folder = fileparts(filename);
6771
assert(isfolder(folder), '%s is not a folder, cannot create %s', folder, filename)
6872

+hdf5nc/ncsize.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
% variable: name of variable inside file
66
%
77
% fsize: vector of variable size per dimension
8-
9-
narginchk(2,2)
10-
validateattributes(filename, {'char'}, {'vector'}, 1)
11-
validateattributes(varname, {'char'}, {'vector'}, 2)
8+
arguments
9+
filename (1,1) string
10+
varname (1,1) string
11+
end
1212

1313
vinf = ncinfo(hdf5nc.expanduser(filename), varname);
1414
fsize = vinf.Size;

0 commit comments

Comments
 (0)